Concept Flow - Public attributes
Create object
Access public attribute
Read or modify attribute value
Use updated value in program
End
This flow shows how a public attribute of an object is created, accessed, and modified directly.
Jump into concepts and practice - no test required
class Car: def __init__(self, color): self.color = color car = Car('red') print(car.color) car.color = 'blue' print(car.color)
| Step | Action | Attribute 'color' Value | Output |
|---|---|---|---|
| 1 | Create Car object with color='red' | red | |
| 2 | Print car.color | red | red |
| 3 | Change car.color to 'blue' | blue | |
| 4 | Print car.color | blue | blue |
| 5 | End of program | blue |
| Variable | Start | After Step 1 | After Step 3 | Final |
|---|---|---|---|---|
| car.color | undefined | red | blue | blue |
Public attributes in Python classes are variables accessible directly from object instances. They can be read or changed freely using dot notation (object.attribute). No special methods are needed to access or modify them. Example: obj.attr = value or print(obj.attr). They provide simple, direct data storage in objects.
name inside a Python class constructor?class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog('Buddy')
print(my_dog.name)age:class Person:
def __init__(self, age):
age = age
p = Person(30)
print(p.age)Car that stores the public attributes make and year. Which code correctly creates these attributes and allows access to them?