Instance Variables - Rybd04/2143-OOP GitHub Wiki

Definition

Instance variable

A variable that is defined in a class but belongs to each individual object (instance) of that class.

Explanation

An instance variable is like how all houses have walls but ever instance of a house might have different colored or sized walls.

Basic Code Example

class Dog:
    def __init__(self, name, breed):
        self.name = name       # instance variable
        self.breed = breed     # instance variable

dog1 = Dog("Buddy", "Golden Retriever")
dog2 = Dog("Max", "Labrador")

print(dog1.name)  # Output: Buddy
print(dog2.name)  # Output: Max

Image

image

Additional Resources

https://stackoverflow.com/questions/743697/what-is-the-exact-definition-of-instance-variable

https://www.dremendo.com/cpp-programming-tutorial/cpp-instance-variables