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
Understanding Public Attributes in Python Classes
📖 Scenario: Imagine you are creating a simple program to manage a library's books. Each book has a title and an author. You want to store this information in a class and be able to access it easily.
🎯 Goal: You will create a Python class with public attributes for the book's title and author. Then, you will create an object of this class and print its attributes.
📋 What You'll Learn
Create a class named Book with public attributes title and author
Create an object of the Book class with the title 'The Great Gatsby' and author 'F. Scott Fitzgerald'
Access and print the title and author attributes of the object
💡 Why This Matters
🌍 Real World
Public attributes are used to store and access information about objects in many programs, such as managing books in a library or products in a store.
💼 Career
Understanding how to use public attributes is a basic skill for programming jobs that involve object-oriented programming, such as software development and data modeling.
Progress0 / 4 steps
1
Create the Book class with public attributes
Create a class called Book with an __init__ method that takes title and author as parameters and assigns them to public attributes self.title and self.author.
Python
Hint
Remember, public attributes are created by assigning values to self.attribute_name inside the __init__ method.
2
Create a Book object with specific title and author
Create an object called my_book from the Book class with the title 'The Great Gatsby' and author 'F. Scott Fitzgerald'.
Python
Hint
Use the class name Book followed by parentheses with the title and author strings inside to create the object.
3
Access the public attributes of the my_book object
Write two lines of code to assign the title attribute of my_book to a variable called book_title and the author attribute to a variable called book_author.
Python
Hint
Use dot notation like object.attribute to access public attributes.
4
Print the book's title and author
Print the values of book_title and book_author on separate lines.
Python
Hint
Use two print() statements, one for each variable.
Practice
(1/5)
1. What is a public attribute in a Python class?
easy
A. An attribute that can be accessed and changed from outside the class
B. An attribute that is hidden and cannot be accessed outside the class
C. A method that runs automatically when an object is created
D. A special function to delete an object
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 A
Quick Check:
Public attribute = accessible outside [OK]
Hint: Public means anyone can access or change it [OK]
Common Mistakes:
Confusing public with private attributes
Thinking methods are attributes
Mixing up constructors with attributes
2. Which of the following is the correct way to create a public attribute name inside a Python class constructor?
easy
A. self->name = value
B. self.name = value
C. name = self.value
D. def name(self):
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 B
Quick Check:
Use self.attribute = value [OK]
Hint: Use self.attribute = value inside __init__ [OK]
Common Mistakes:
Using def instead of assignment
Wrong arrow syntax like self->name
Assigning attribute to name instead of self.name
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)
medium
A. Dog
B. Error
C. my_dog
D. Buddy
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 D
Quick Check:
Print attribute value = Buddy [OK]
Hint: Print object.attribute to see stored value [OK]
Common Mistakes:
Expecting class name instead of attribute value
Confusing object name with attribute
Thinking it causes an error
4. Find the error in this code that tries to create a public attribute age:
class Person:
def __init__(self, age):
age = age
p = Person(30)
print(p.age)
medium
A. The attribute should be assigned to self.age, not age
B. The print statement should be print(age)
C. The constructor is missing a return statement
D. The class name should be lowercase
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 A
Quick Check:
Use self.attribute = value to create public attribute [OK]
Hint: Always assign attributes to self.attribute inside __init__ [OK]
Common Mistakes:
Assigning to local variable instead of self.attribute
Trying to print variable not attached to object
Thinking constructor needs return
5. You want to create a class Car that stores the public attributes make and year. Which code correctly creates these attributes and allows access to them?
hard
A. class Car:
def __init__(self, make, year):
self.make = make
year = self.year
my_car = Car('Toyota', 2020)
print(my_car.make, my_car.year)
B. class Car:
def __init__(self, make, year):
make = self.make
year = self.year
my_car = Car('Toyota', 2020)
print(my_car.make, my_car.year)
C. 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)
D. class Car:
def __init__(self, make, year):
self.make = make
self.year = year
my_car = Car('Toyota', 2020)
print(make, year)
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 C
Quick Check:
Assign and access via self.attribute and object.attribute [OK]
Hint: Assign attributes to self and access via object.attribute [OK]