Introduction
Custom attributes let you add extra information to objects. This helps you store and use data that fits your needs.
Jump into concepts and practice - no test required
Custom attributes let you add extra information to objects. This helps you store and use data that fits your needs.
object.attribute_name = value
You can add any attribute name you want.
The value can be any data type like number, text, or even another object.
color with value 'red' to the my_car object.class Car: pass my_car = Car() my_car.color = 'red'
age is added after the object is created.class Person: def __init__(self, name): self.name = name p = Person('Anna') p.age = 30
This program creates a Book object with a title. Then it adds a custom attribute author and prints both.
class Book: def __init__(self, title): self.title = title my_book = Book('Python Basics') my_book.author = 'Sam' print(f"Title: {my_book.title}") print(f"Author: {my_book.author}")
You can add custom attributes anytime after creating the object.
Be careful not to overwrite existing attributes unless you want to change them.
Custom attributes let you add extra data to objects.
Use object.attribute = value to add them.
This helps keep related information together in one place.
color with value 'red' to an object car?class Dog:
pass
my_dog = Dog()
my_dog.age = 5
print(my_dog.age)height to an object person:class Person:
def __init__(self, name):
self.name = name
person = Person('Alice')
person.height = 170
print(person['height'])status to multiple instances of a class Task only if their priority is above 5. Which code correctly does this?