In this tutorial, we will explore one of the fundamental concepts of object-oriented programming (OOP) in Python: classes, constructors (__init__
method), and the self
parameter. Understanding these concepts is crucial for writing robust, organized, and maintainable code.
Introduction to Classes
A class is a template or blueprint that defines the characteristics and behaviors of an object. It’s essentially a design pattern or a template that defines the properties and methods of an object. In Python, classes are defined using the class
keyword followed by the name of the class and a colon (:).
class MyClass:
pass
Constructors (__init__
method)
The __init__
method is a special method that’s automatically called when an object of a class is instantiated. It’s used to initialize the attributes of the class. The __init__
method is also known as the constructor.
class MyClass:
def __init__(self):
print("Object created")
When you create an instance of the MyClass
class, the __init__
method will be called automatically:
obj = MyClass() # Output: Object created
The __init__
method can also take parameters to initialize the attributes of the class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person = Person("John", 30)
print(person.name) # Output: John
print(person.age) # Output: 30
The self
Parameter
The self
parameter is a reference to the current instance of the class and is used to access variables that belongs to the class. It’s a convention in Python to use the name self
for this parameter.
class MyClass:
def __init__(self):
self.attribute = "Hello"
def method(self):
print(self.attribute)
obj = MyClass()
obj.method() # Output: Hello
In this example, the self
parameter is used to access the attribute
variable that belongs to the class.
Class Attributes vs. Instance Attributes
Class attributes are shared by all instances of a class, while instance attributes are unique to each instance:
class MyClass:
attribute = "Hello" # Class attribute
def __init__(self):
self.instance_attribute = "World" # Instance attribute
obj1 = MyClass()
obj2 = MyClass()
print(MyClass.attribute) # Output: Hello
print(obj1.attribute) # Output: Hello
print(obj2.attribute) # Output: Hello
print(obj1.instance_attribute) # Output: World
print(obj2.instance_attribute) # Output: World
Example Use Case
Here’s an example use case that demonstrates the concepts of classes, constructors, and self
:
class Bill:
def __init__(self, apples, figs, dates):
self.apples = apples
self.figs = figs
self.dates = dates
self.bill = apples + figs + dates
print("Buy", self.apples, "apples,", self.figs, "figs, and", self.dates, "dates. Total bill is", self.bill)
purchase = Bill(5, 6, 7)
# Output: Buy 5 apples, 6 figs, and 7 dates. Total bill is 18
In this example, the Bill
class has an __init__
method that initializes the attributes of the class, including the total bill. The self
parameter is used to access these attributes.
Conclusion
In conclusion, understanding classes, constructors (__init__
method), and the self
parameter is essential for writing object-oriented code in Python. By using these concepts, you can create robust, organized, and maintainable code that’s easy to understand and extend.