In Python's object-oriented programming model, attributes and methods are fundamental building blocks of a class. They help define the structure and behavior of the objects you create. Attributes store the data related to the object, while methods define what the object can do.
1. What Are Attributes?
Attributes are variables that belong to an object. They are used to store information about the object's state.
Instance Attributes
These are attributes unique to each object and are usually defined in the __init__()
method using self
.
class Person:
def __init__(self, name, age):
self.name = name # instance attribute
self.age = age
person1 = Person("Alice", 30)
print(person1.name) # Output: Alice
print(person1.age) # Output: 30
Class Attributes
These are shared across all instances of the class.
class Dog:
species = "Canine" # class attribute
def __init__(self, name):
self.name = name
dog1 = Dog("Buddy")
dog2 = Dog("Max")
print(dog1.species) # Output: Canine
print(dog2.species) # Output: Canine
2. What Are Methods?
Methods are functions defined inside a class that operate on object data. They always take self
as the first parameter to refer to the current object.
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
circle1 = Circle(5)
print(circle1.area()) # Output: 78.5
3. Special Methods (Magic Methods)
These methods start and end with double underscores, like __init__
, __str__
, and __len__
.
class Book:
def __init__(self, title):
self.title = title
def __str__(self):
return f"Book Title: {self.title}"
book = Book("Python 101")
print(book) # Output: Book Title: Python 101
4. Accessing and Modifying Attributes
class Student:
def __init__(self, name):
self.name = name
student1 = Student("Rahul")
print(student1.name) # Output: Rahul
student1.name = "Aman"
print(student1.name) # Output: Aman
5. Private Attributes
Use a single underscore (convention) or double underscore (name mangling) to indicate private attributes.
class BankAccount:
def __init__(self, balance):
self.__balance = balance # private attribute
def get_balance(self):
return self.__balance
account = BankAccount(1000)
print(account.get_balance()) # Output: 1000
6. Calling Methods from Other Methods
class Temperature:
def __init__(self, celsius):
self.celsius = celsius
def to_fahrenheit(self):
return (self.celsius * 9/5) + 32
def display(self):
print(f"{self.celsius}°C = {self.to_fahrenheit()}°F")
temp = Temperature(25)
temp.display() # Output: 25°C = 77.0°F
7. Summary Table
Concept | Description | Example |
---|---|---|
Instance Attribute | Unique to each object | self.name = name |
Class Attribute | Shared across all objects | species = "Canine" |
Method | Function inside class | def greet(self): |
Private Attribute | Intended for internal use | self.__balance |
8. Final Thoughts
Understanding attributes and methods is crucial to mastering object-oriented programming in Python. Attributes define the data an object holds, while methods define how that data is manipulated or used. By writing clean, modular classes, you can build scalable and maintainable applications with ease.