Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do we reference Python class attributes?
In Python, the variables that are declared inside a class are known as attributes. These attributes can be accessed and modified by both the class and its objects. There are two types of attributes ?
- Class Attributes: Variables defined at the class level that are shared among all instances of the class. They can be accessed using the class name or instance objects.
- Instance Attributes: Variables defined inside the __init__() method or other instance methods that are specific to each object. They are accessed using the object name.
Accessing Class Attributes
Class attributes can be accessed using either ClassName.attribute or instance.attribute syntax ?
class Square:
sides = 4 # Class attribute
shape_type = "polygon" # Class attribute
# Accessing via class name
print("Using class name:")
print(Square.sides)
print(Square.shape_type)
# Accessing via instance
obj1 = Square()
print("\nUsing instance:")
print(obj1.sides)
print(obj1.shape_type)
Using class name: 4 polygon Using instance: 4 polygon
Modifying Class Attributes
Class attributes can be modified by assigning new values through the class name. This change affects all instances ?
class Square:
sides = 4
shape_type = "polygon"
obj1 = Square()
obj2 = Square()
print("Before modification:")
print(f"obj1.sides: {obj1.sides}")
print(f"obj2.sides: {obj2.sides}")
# Modify class attribute
Square.sides = 8
print("\nAfter modification:")
print(f"obj1.sides: {obj1.sides}")
print(f"obj2.sides: {obj2.sides}")
Before modification: obj1.sides: 4 obj2.sides: 4 After modification: obj1.sides: 8 obj2.sides: 8
Accessing Instance Attributes
Instance attributes are typically defined in the __init__() method and are accessed using object_name.attribute syntax ?
class Square:
shape_type = "polygon" # Class attribute
def __init__(self, side_length):
self.side_length = side_length # Instance attribute
self.area = side_length ** 2 # Instance attribute
# Create instances
square1 = Square(5)
square2 = Square(10)
print(f"Square1 - Side: {square1.side_length}, Area: {square1.area}")
print(f"Square2 - Side: {square2.side_length}, Area: {square2.area}")
Square1 - Side: 5, Area: 25 Square2 - Side: 10, Area: 100
Modifying Instance Attributes
Instance attributes can be modified by assigning new values through the object name. This only affects the specific instance ?
class Square:
def __init__(self, side_length):
self.side_length = side_length
self.area = side_length ** 2
square1 = Square(5)
square2 = Square(10)
print("Before modification:")
print(f"Square1 - Side: {square1.side_length}, Area: {square1.area}")
print(f"Square2 - Side: {square2.side_length}, Area: {square2.area}")
# Modify only square1
square1.side_length = 7
square1.area = square1.side_length ** 2
print("\nAfter modifying square1:")
print(f"Square1 - Side: {square1.side_length}, Area: {square1.area}")
print(f"Square2 - Side: {square2.side_length}, Area: {square2.area}")
Before modification: Square1 - Side: 5, Area: 25 Square2 - Side: 10, Area: 100 After modifying square1: Square1 - Side: 7, Area: 49 Square2 - Side: 10, Area: 100
Comparison
| Attribute Type | Access Method | Shared Among Instances | Best For |
|---|---|---|---|
| Class Attribute |
ClassName.attr or instance.attr
|
Yes | Constants, default values |
| Instance Attribute | instance.attr |
No | Object-specific data |
Conclusion
Use class attributes for data shared across all instances, and instance attributes for object-specific data. Access both types using dot notation, but remember that class attribute changes affect all instances while instance attribute changes are isolated to individual objects.
