Attributes
Part of the Object Oriented Programming section of Coddy's Python journey — lesson 6 of 64.
Attributes are data or variables that belong to a class or its objects. They store information about the object's state.
There are two types of attributes:
Class attributes - shared by all objects of the class:
class Student:
school_name = "Python Academy" # class attributeInstance attributes - unique to each object:
class Student:
school_name = "Python Academy"
def set_info(self, name, age):
self.name = name # instance attribute
self.age = age # instance attributeCreate student objects and set their individual data:
student1 = Student()
student2 = Student()
student1.set_info("Alice", 20)
student2.set_info("Bob", 22)Access instance attributes (unique to each object):
print(student1.name) # Alice
print(student2.name) # Bob
print(student1.age) # 20Access class attributes (same for all objects):
print(student1.school_name) # Python Academy
print(student2.school_name) # Python Academy
print(Student.school_name) # Python AcademyOutput:
Alice
Bob
20
Python Academy
Python Academy
Python AcademyKey Difference: Class attributes are shared by all objects, while instance attributes are unique to each object. Use self.attribute_name for instance attributes and just attribute_name for class attributes.
Cheat sheet
Attributes are data or variables that belong to a class or its objects.
Class attributes - shared by all objects of the class:
class Student:
school_name = "Python Academy" # class attributeInstance attributes - unique to each object, defined using self:
class Student:
school_name = "Python Academy"
def set_info(self, name, age):
self.name = name # instance attribute
self.age = age # instance attributeAccess instance attributes:
student1 = Student()
student1.set_info("Alice", 20)
print(student1.name) # AliceAccess class attributes:
print(student1.school_name) # Python Academy
print(Student.school_name) # Python AcademyTry it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self ParameterMethodsAttributesConstructor Method (__init__)Recap - Simple Calculator4Inheritance
Basic InheritanceThe super() FunctionMethod OverridingMultiple InheritanceMethod Resolution OrderRecap - Employee Hierarchy7Special Methods
Magic Methods IntroductionOperator OverloadingContainer Magic MethodsRecap - Custom List10Design Patterns Part 1
Intro to design patternSingleton PatternFactory PatternObserver PatternStrategy Pattern2Decorators
Introduction to DecoratorsProperty DecoratorStatic Method DecoratorClass Method Decorator5Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceMixinsStatic and Class MethodsClass DecoratorsContext Managers3Class Properties
Instance vs Class VariablesProperty DecoratorsPrivate AttributesRecap - Bank Account Manager6Encapsulation
Public, Protected, Private MemAccess ModifiersInformation HidingProperty Decorators AdvancedRecap - Student Records System12Project: Library Management
Project OverviewBook and User Classes