Skip to Content
Course content

5.2.1 Instance variables and methods

In Python, instance variables and instance methods are key concepts when working with object-oriented programming (OOP). These define the properties and behaviors of an object created from a class.

Instance Variables

Instance variables are variables that are specific to an object (instance) of a class. They are used to store the data or state of an object. These variables are usually initialized inside the __init__ constructor method of the class using the self keyword. Each instance of a class can have its own values for these variables.

Example of Instance Variables:
class Car:
    def __init__(self, make, model, year):
        self.make = make  # Instance variable
        self.model = model  # Instance variable
        self.year = year  # Instance variable

# Creating two instances of the Car class
car1 = Car("Toyota", "Corolla", 2020)
car2 = Car("Honda", "Civic", 2022)

# Accessing instance variables
print(car1.make)   # Output: Toyota
print(car2.year)   # Output: 2022
  • In the example, make, model, and year are instance variables because they are specific to each object (car1 and car2).
  • These variables are created using self, and each object can have its own unique value for these attributes.

Instance Methods

Instance methods are functions defined within a class that operate on instance variables. They typically have the self parameter as the first argument, which allows them to access and modify the attributes of the instance (object) they belong to.

Instance methods define the behaviors or actions that can be performed on the object's attributes.

Example of Instance Methods:
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    # Instance method to display car details
    def display_details(self):
        print(f"Car Details: {self.year} {self.make} {self.model}")

    # Instance method to check car age
    def car_age(self, current_year):
        age = current_year - self.year
        return age

# Creating an instance of the Car class
car1 = Car("Toyota", "Corolla", 2020)

# Calling instance methods
car1.display_details()  # Output: Car Details: 2020 Toyota Corolla
print(f"Car age: {car1.car_age(2024)} years")  # Output: Car age: 4 years
  • display_details() and car_age() are instance methods because they operate on instance variables (make, model, year) and use the self keyword to refer to the object’s specific attributes.
  • The car_age() method calculates the age of the car by subtracting the year of manufacture from the current year.

Key Points About Instance Variables and Methods:

  1. Instance Variables:
    • Created using the self keyword within the __init__ constructor.
    • Each object created from the class has its own set of instance variables.
    • Represent the state or data of the object.
  2. Instance Methods:
    • Defined using the def keyword within the class.
    • The first argument is always self, which refers to the current object.
    • Operate on instance variables and represent the behavior or actions associated with the object.

Advantages of Instance Variables and Methods:

  • Encapsulation: Instance variables help encapsulate the object's state, ensuring that its data is protected and can only be accessed or modified through methods.
  • Reusability: Instance methods allow the same class to be reused to create multiple objects, each with its own data and behavior.
  • Modularity: By defining instance methods, classes can have distinct functionalities, making the code modular and easy to maintain.

In summary, instance variables store the data associated with a specific object, while instance methods define the behaviors or actions that an object can perform using its data. Both are central to the concept of object-oriented programming in Python.

Commenting is not enabled on this course.