Public attributes let you store and access information inside an object easily. They are open for anyone to read or change.
Public attributes in Python
Start learning this pattern below
Jump into concepts and practice - no test required
class ClassName: def __init__(self, attribute1, attribute2): self.attribute1 = attribute1 # public attribute self.attribute2 = attribute2 # public attribute
Public attributes are defined inside the __init__ method using self.attribute_name.
They can be accessed or changed from outside the class using object.attribute_name.
Dog class with public attributes name and age. We create a dog and access/change its attributes directly.class Dog: def __init__(self, name, age): self.name = name # public attribute self.age = age # public attribute my_dog = Dog('Buddy', 3) print(my_dog.name) # prints Buddy my_dog.age = 4 print(my_dog.age) # prints 4
Car class has a public attribute color. We change the car's color after creating it.class Car: def __init__(self, color): self.color = color # public attribute car1 = Car('red') print(car1.color) # prints red car1.color = 'blue' print(car1.color) # prints blue
This program creates a Person object with public attributes name and age. It prints the values, changes the age, and prints the new age.
class Person: def __init__(self, name, age): self.name = name # public attribute self.age = age # public attribute person1 = Person('Alice', 30) print(f'Name: {person1.name}') print(f'Age: {person1.age}') # Changing public attribute person1.age = 31 print(f'New Age: {person1.age}')
Public attributes can be accessed and changed from anywhere in your code.
Be careful when changing public attributes because it can affect other parts of your program.
For more control, you can learn about private attributes later, but public attributes are great for simple use.
Public attributes store information inside objects that anyone can access or change.
They are easy to use and good for simple data storage in classes.
Use self.attribute_name inside __init__ to create public attributes.
Practice
Solution
Step 1: Understand attribute visibility
Public attributes are designed to be accessed and modified from outside the class.Step 2: Compare options
An attribute that can be accessed and changed from outside the class correctly describes public attributes. Options A, B, and D describe other concepts.Final Answer:
An attribute that can be accessed and changed from outside the class -> Option AQuick Check:
Public attribute = accessible outside [OK]
- Confusing public with private attributes
- Thinking methods are attributes
- Mixing up constructors with attributes
name inside a Python class constructor?Solution
Step 1: Recall syntax for public attributes
Inside __init__, public attributes are created by assigning to self.attribute_name.Step 2: Check each option
self.name = value uses correct syntax: self.name = value. Others are invalid Python syntax or wrong usage.Final Answer:
self.name = value -> Option BQuick Check:
Use self.attribute = value [OK]
- Using def instead of assignment
- Wrong arrow syntax like self->name
- Assigning attribute to name instead of self.name
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog('Buddy')
print(my_dog.name)Solution
Step 1: Understand attribute assignment
The constructor sets self.name to the value passed, which is 'Buddy'.Step 2: Print the attribute value
Printing my_dog.name outputs 'Buddy'.Final Answer:
Buddy -> Option DQuick Check:
Print attribute value = Buddy [OK]
- Expecting class name instead of attribute value
- Confusing object name with attribute
- Thinking it causes an error
age:class Person:
def __init__(self, age):
age = age
p = Person(30)
print(p.age)Solution
Step 1: Check attribute assignment
Assigning age = age only creates a local variable, not an attribute of the object.Step 2: Correct attribute assignment
It should be self.age = age to create a public attribute accessible outside.Final Answer:
The attribute should be assigned to self.age, not age -> Option AQuick Check:
Use self.attribute = value to create public attribute [OK]
- Assigning to local variable instead of self.attribute
- Trying to print variable not attached to object
- Thinking constructor needs return
Car that stores the public attributes make and year. Which code correctly creates these attributes and allows access to them?Solution
Step 1: Check attribute assignments
class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) correctly assigns self.make and self.year to the passed values.Step 2: Check attribute access
class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) prints my_car.make and my_car.year, which are valid public attributes.Final Answer:
class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) -> Option CQuick Check:
Assign and access via self.attribute and object.attribute [OK]
- Assigning attributes backwards (make = self.make)
- Accessing attributes without object prefix
- Mixing local variables with attributes
