Bird
Raised Fist0
Pythonprogramming~10 mins

Instance attributes in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Instance attributes
Create object from class
Call __init__ method
Set instance attributes
Object ready with own data
Access or modify attributes
Use attributes in methods or outside
When you create an object, Python runs __init__ to set up instance attributes that hold data unique to that object.
Execution Sample
Python
class Dog:
    def __init__(self, name):
        self.name = name

d = Dog('Buddy')
print(d.name)
This code creates a Dog object with a name attribute and prints it.
Execution Table
StepActionEvaluationResult
1Create Dog object d with name 'Buddy'Calls Dog.__init__(d, 'Buddy')d.name set to 'Buddy'
2Access d.nameLook up attribute 'name' on d'Buddy'
3Print d.nameOutput the string stored in d.nameBuddy
💡 Program ends after printing the instance attribute value
Variable Tracker
VariableStartAfter Step 1After Step 2Final
dundefinedDog object with name='Buddy'Dog object with name='Buddy'Dog object with name='Buddy'
d.nameundefined'Buddy''Buddy''Buddy'
Key Moments - 2 Insights
Why do we use self.name = name inside __init__?
self.name = name sets the instance attribute 'name' for that specific object. See execution_table step 1 where d.name is set to 'Buddy'. Without self, the name would be just a local variable.
Can different objects have different values for the same attribute?
Yes! Each object has its own instance attributes. If you create another Dog with a different name, it will have its own separate name. This is shown by how d.name holds 'Buddy' only for that object.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 1, what does self.name get set to?
AThe class Dog
B'self'
C'Buddy'
DUndefined
💡 Hint
Check the 'Result' column in step 1 where d.name is set to 'Buddy'
At which step is the instance attribute 'name' accessed to print?
AStep 2
BStep 3
CStep 1
DNone
💡 Hint
Look at the 'Action' column in step 3 where d.name is printed
If we create another Dog object with name 'Max', what will d.name be?
A'Buddy'
B'Max'
CUndefined
DError
💡 Hint
Instance attributes belong to each object separately as shown in variable_tracker for d.name
Concept Snapshot
Instance attributes are variables tied to an object.
They are set inside __init__ using self.attribute = value.
Each object has its own copy of these attributes.
Access them with object.attribute syntax.
They store data unique to each object.
Full Transcript
When you create an object from a class in Python, the __init__ method runs automatically. Inside __init__, you use self.attribute = value to create instance attributes. These attributes belong to that specific object only. For example, when we create a Dog object with name 'Buddy', the attribute name is set to 'Buddy' for that object. You can access this attribute later using object.attribute, like d.name. Different objects can have different values for the same attribute name. This lets each object keep its own data separate.

Practice

(1/5)
1. What is an instance attribute in Python classes?
easy
A. A variable shared by all objects of the class
B. A function that belongs to the class
C. A method to create new objects
D. A variable that stores data unique to each object

Solution

  1. Step 1: Understand instance attributes

    Instance attributes are variables that belong to each object separately, not shared.
  2. Step 2: Differentiate from class attributes

    Class attributes are shared by all objects, but instance attributes hold unique data per object.
  3. Final Answer:

    A variable that stores data unique to each object -> Option D
  4. Quick Check:

    Instance attribute = unique data per object [OK]
Hint: Instance attributes belong to objects, not the class itself [OK]
Common Mistakes:
  • Confusing instance attributes with class attributes
  • Thinking methods are attributes
  • Assuming all objects share the same attribute values
2. Which of the following is the correct way to define an instance attribute inside a class?
easy
A. name = "Alice" outside any method
B. def name(self): return "Alice"
C. self.name = "Alice" inside __init__ method
D. class.name = "Alice" inside __init__

Solution

  1. Step 1: Identify instance attribute syntax

    Instance attributes are set inside __init__ using self.attribute = value.
  2. Step 2: Check each option

    self.name = "Alice" inside __init__ method uses self.name = "Alice" inside __init__, which is correct. Others are class attributes, methods, or invalid.
  3. Final Answer:

    self.name = "Alice" inside __init__ method -> Option C
  4. Quick Check:

    Instance attribute = self.attribute inside __init__ [OK]
Hint: Use self.attribute = value inside __init__ for instance attributes [OK]
Common Mistakes:
  • Defining attributes outside __init__ without self
  • Using class.attribute instead of self.attribute
  • Confusing methods with attributes
3. What will be the output of this code?
class Dog:
    def __init__(self, name):
        self.name = name

dog1 = Dog("Buddy")
dog2 = Dog("Max")
print(dog1.name)
print(dog2.name)
medium
A. Buddy Max
B. Max Buddy
C. Buddy Buddy
D. Max Max

Solution

  1. Step 1: Understand instance attribute assignment

    dog1.name is set to "Buddy" and dog2.name is set to "Max" separately.
  2. Step 2: Print instance attributes

    Printing dog1.name outputs "Buddy" and dog2.name outputs "Max".
  3. Final Answer:

    Buddy Max -> Option A
  4. Quick Check:

    Each object has its own name attribute [OK]
Hint: Each object keeps its own attribute values [OK]
Common Mistakes:
  • Assuming all objects share the same attribute
  • Mixing up the order of print statements
  • Confusing class and instance attributes
4. Find the error in this code:
class Car:
    def __init__(self, model):
        model = model

car = Car("Tesla")
print(car.model)
medium
A. AttributeError because model is not set as instance attribute
B. SyntaxError due to missing self
C. TypeError because __init__ has wrong parameters
D. No error, prints Tesla

Solution

  1. Step 1: Check attribute assignment in __init__

    The code assigns model = model, which only assigns local variable, not instance attribute.
  2. Step 2: Accessing car.model causes error

    Since self.model is never set, car.model does not exist, causing AttributeError.
  3. Final Answer:

    AttributeError because model is not set as instance attribute -> Option A
  4. Quick Check:

    Use self.model = model to set instance attribute [OK]
Hint: Always use self.attribute = value to set instance attributes [OK]
Common Mistakes:
  • Forgetting self. when assigning attributes
  • Assuming local variable sets instance attribute
  • Expecting attribute to exist without self
5. You want to create a class Book where each book has a title and a list of authors. How do you correctly set instance attributes so each book has its own authors list without sharing it between objects?
hard
A. Set self.authors = None and assign list later
B. Set self.authors = [] inside __init__ method
C. Set self.authors = authors where authors is a default empty list in parameters
D. Set authors = [] as a class attribute outside methods

Solution

  1. Step 1: Avoid shared mutable class attributes

    Setting authors = [] as class attribute shares the same list across all objects, causing bugs.
  2. Step 2: Initialize instance attribute inside __init__

    Setting self.authors = [] inside __init__ creates a new list for each object, avoiding sharing.
  3. Final Answer:

    Set self.authors = [] inside __init__ method -> Option B
  4. Quick Check:

    Mutable instance attributes must be set inside __init__ [OK]
Hint: Initialize mutable attributes inside __init__ to avoid sharing [OK]
Common Mistakes:
  • Using mutable default arguments in method parameters
  • Defining mutable attributes as class variables
  • Not initializing mutable attributes per instance