Bird
Raised Fist0
Pythonprogramming~10 mins

Accessing and modifying 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 - Accessing and modifying attributes
Create object
Access attribute
Read or use value
Modify attribute
Use updated value
End
This flow shows creating an object, accessing its attribute, reading or using it, modifying the attribute, and then using the updated value.
Execution Sample
Python
class Car:
    def __init__(self, color):
        self.color = color

car = Car('red')
print(car.color)
car.color = 'blue'
print(car.color)
This code creates a Car object with a color attribute, prints it, changes the color, and prints the new color.
Execution Table
StepActionAttribute Access/ModificationValueOutput
1Create Car object with color 'red'car.colorred
2Print car.colorcar.colorredred
3Modify car.color to 'blue'car.colorblue
4Print car.colorcar.colorblueblue
5End of code
💡 Code ends after printing updated attribute value.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
car.colorundefinedredblueblue
Key Moments - 2 Insights
Why does car.color print 'red' before modification and 'blue' after?
Because at step 1, car.color is set to 'red'. At step 3, it is changed to 'blue'. The execution_table rows 2 and 4 show the printed values before and after modification.
Can we access car.color before creating the object?
No, the object must exist first. The execution_table shows car.color is undefined before step 1 when the object is created.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of car.color at step 3?
A'blue'
B'red'
Cundefined
D'green'
💡 Hint
Check the 'Value' column at step 3 in the execution_table.
At which step does car.color change from 'red' to 'blue'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Value' columns in the execution_table.
If we print car.color before step 1, what would happen?
AIt prints 'red'
BIt prints 'blue'
CIt causes an error
DIt prints nothing
💡 Hint
Refer to key_moments about accessing attributes before object creation.
Concept Snapshot
Access attributes with dot notation: object.attribute
Modify by assignment: object.attribute = new_value
Must create object before access
Printing shows current attribute value
Changes affect future accesses
Full Transcript
This example shows how to access and modify attributes in Python. First, a Car object is created with a color attribute set to 'red'. When we print car.color, it shows 'red'. Then we change car.color to 'blue'. Printing again shows the updated value 'blue'. The execution table tracks each step, showing the attribute's value before and after modification. Beginners often wonder why the value changes; it changes because we assign a new value. Also, trying to access an attribute before creating the object causes an error. Remember to create objects before accessing their attributes.

Practice

(1/5)
1. What is the correct way to access the attribute color of an object car in Python?
easy
A. car.color
B. car[color]
C. car->color
D. car[color()]

Solution

  1. Step 1: Understand attribute access syntax

    In Python, attributes of an object are accessed using dot notation: object.attribute.
  2. Step 2: Apply to given object and attribute

    For object car and attribute color, the correct syntax is car.color.
  3. Final Answer:

    car.color -> Option A
  4. Quick Check:

    Dot notation accesses attributes = car.color [OK]
Hint: Use dot (.) to access attributes on objects [OK]
Common Mistakes:
  • Using brackets like car[color]
  • Using arrow notation like car->color
  • Calling attribute as a function like car[color()]
2. Which of the following is the correct syntax to change the attribute age of an object person to 30?
easy
A. person["age"] = 30
B. person.age = 30
C. person->age = 30
D. person.age(30)

Solution

  1. Step 1: Recall attribute assignment syntax

    To modify an attribute, use dot notation with assignment: object.attribute = value.
  2. Step 2: Apply to given object and attribute

    Set person.age to 30 by writing person.age = 30.
  3. Final Answer:

    person.age = 30 -> Option B
  4. Quick Check:

    Assign attribute with dot and equals = person.age = 30 [OK]
Hint: Use dot and equals to set attribute: obj.attr = value [OK]
Common Mistakes:
  • Using brackets like person["age"] = 30
  • Using arrow notation person->age = 30
  • Trying to call attribute like a function person.age(30)
3. What will be the output of this code?
class Dog:
    def __init__(self, name):
        self.name = name

my_dog = Dog("Buddy")
print(my_dog.name)
my_dog.name = "Max"
print(my_dog.name)
medium
A. Buddy\nBuddy
B. Max\nBuddy
C. Max\nMax
D. Buddy\nMax

Solution

  1. Step 1: Understand initial attribute value

    The constructor sets self.name to "Buddy". So, my_dog.name is initially "Buddy".
  2. Step 2: Modify attribute and print again

    After printing "Buddy", the code sets my_dog.name = "Max". The second print outputs "Max".
  3. Final Answer:

    Buddy Max -> Option D
  4. Quick Check:

    Initial then changed attribute prints = Buddy Max [OK]
Hint: Changing attribute updates value printed next [OK]
Common Mistakes:
  • Thinking attribute change does not affect output
  • Confusing order of prints
  • Assuming attribute is immutable
4. Identify the error in this code snippet:
class Cat:
    def __init__(self, color):
        self.color = color

kitty = Cat("black")
print(kitty[color])
medium
A. Using brackets instead of dot to access attribute
B. Missing parentheses in class definition
C. Incorrect constructor name
D. Attribute 'color' not defined

Solution

  1. Step 1: Check attribute access syntax

    The code uses kitty[color], which tries to access like a dictionary key, but color is an attribute, not a key.
  2. Step 2: Correct syntax for attribute access

    Use dot notation: kitty.color to access the attribute.
  3. Final Answer:

    Using brackets instead of dot to access attribute -> Option A
  4. Quick Check:

    Attributes use dot, not brackets = Using brackets instead of dot to access attribute [OK]
Hint: Use dot, not brackets, to access attributes [OK]
Common Mistakes:
  • Using brackets like kitty[color]
  • Thinking attributes are dictionary keys
  • Confusing attribute access with indexing
5. Given this class:
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

book1 = Book("1984", "Orwell")
book2 = Book("Animal Farm", "Orwell")

# Change author of book2 to "George Orwell"

Which code correctly updates book2 author without affecting book1?
hard
A. book2["author"] = "George Orwell"
B. Book.author = "George Orwell"
C. book2.author = "George Orwell"
D. book1.author = "George Orwell"

Solution

  1. Step 1: Understand instance vs class attributes

    Changing book2.author modifies only that instance's attribute, not book1.
  2. Step 2: Avoid changing class attribute or other instance

    Assigning Book.author changes class attribute for all instances; changing book1.author affects wrong object; brackets are invalid syntax.
  3. Final Answer:

    book2.author = "George Orwell" -> Option C
  4. Quick Check:

    Set instance attribute with dot on correct object = book2.author = "George Orwell" [OK]
Hint: Assign attribute on specific object with dot notation [OK]
Common Mistakes:
  • Changing class attribute instead of instance attribute
  • Modifying wrong object's attribute
  • Using brackets instead of dot notation