Exploring Python Classes and Object-Oriented Programming
Understanding Classes, Inheritance, Encapsulation, and Static Methods in Python
In Python, a class is like a blueprint or a template that you can use to create objects.
Imagine you want to make a toy robot. You can have a blueprint that tells you what the robot should look like, what it can do, and what kind of parts it has. That blueprint is similar to a class in Python. You can create many robot toys from that one blueprint.
Example 1: Simple Class
In this simple example, we define a class Dog
. It has an __init__
method to initialize the dog's name, and a bark
method that returns a message.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says Woof!"
Example 2: Creating a Simple Class
In this example, we define a Dog
class with a constructor (__init__
) method to initialize the name
and breed
attributes. The bark
method is used to make the dog bark. We create two Dog
objects (instances) and call the bark
method on them.
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
return f"{self.name} says Woof!"
# Creating instances of the Dog class
dog1 = Dog("Buddy", "Golden Retriever")
dog2 = Dog("Molly", "Labrador")
print(dog1.bark()) # Output: Buddy says Woof!
print(dog2.bark()) # Output: Molly says Woof!
Example 3: Class Inheritance
Here, we have a base class Animal
with a common attribute name
. The Cat
class inherits from Animal
and adds its own method, meow
. When we create a Cat
object, it can access both name
and meow
methods.
class Animal:
def __init__(self, name):
self.name = name
class Cat(Animal):
def meow(self):
return f"{self.name} says Meow!"
cat = Cat("Whiskers")
print(cat.meow()) # Output: Whiskers says Meow!
Example 4: Class Variables and Methods
In this example, we define a class variable pi
that's shared among all instances. The Circle
class calculates the area of a circle using the class variable and an instance variable radius
.
class Circle:
pi = 3.14159
def __init__(self, radius):
self.radius = radius
def area(self):
return self.pi * self.radius**2
circle = Circle(5)
print(circle.area()) # Output: 78.53975
Example 5: Private Attributes
In the Python code snippet you provided, the double underscore __
before attribute names (__account_number
and __balance
) is used to indicate that these attributes are intended to be private. This means that they are not meant to be accessed directly from outside the class, and their values should be accessed or modified through methods provided by the class.
class BankAccount:
def __init__(self, account_number, balance):
self.__account_number = account_number
self.__balance = balance
def get_balance(self):
return self.__balance
def deposit(self, amount):
self.__balance += amount
__init__
Method:
- The
__init__
method is the constructor of theBankAccount
class. It takes two parameters,account_number
andbalance
, which are used to initialize the private attributes__account_number
and__balance
. - By prefixing these attribute names with double underscores, you are indicating that they should not be accessed directly from outside the class.
2. get_balance
Method:
- The
get_balance
method is a public method that provides a way to retrieve the value of the private attribute__balance
. - Since
__balance
is private, it cannot be accessed directly usinginstance.__balance
. Instead, you use theget_balance
method to access its value.
3. deposit
Method:
- The
deposit
method is also a public method that allows you to deposit money into the bank account by adding the specifiedamount
to the private attribute__balance
. - Like
get_balance
, thedeposit
method is used to access and modify the__balance
attribute indirectly.
Example 6 : Getter and Setter Methods
Here, we use getter and setter methods (get_age
and set_age
) to control access to the private attribute _age
. This helps enforce data encapsulation.
class Student:
def __init__(self, name, age):
self._name = name # Private attribute
self._age = age # Private attribute
def get_age(self):
return self._age
def set_age(self, age):
if age >= 0:
self._age = age
- Constructor (
__init__
) Method: The__init__
method is the constructor for theStudent
class. It initializes the attributes_name
and_age
with values passed as arguments (name
andage
). - Private Attributes: The attributes
_name
and_age
are marked as "private" by convention using a single underscore prefix (e.g.,_name
,_age
). This convention suggests to other programmers that these attributes should not be accessed directly from outside the class. - Getter Method (
get_age
): Theget_age
method is a public method that allows external code to retrieve the value of the private_age
attribute. It provides controlled access to the age attribute. - Setter Method (
set_age
): Theset_age
method is a public method used to modify the value of the_age
attribute. Before updating the age, it checks if the new age provided is non-negative (age >= 0). This is a simple form of validation to ensure that the age remains a non-negative integer. - Creating an Instance of the
Student
Class: An instance of theStudent
class is created with the name "Alice" and an initial age of 20. - Using Getter and Setter Methods: The
get_age
method is used to retrieve and print the student's age, which initially outputs20
. Theset_age
method is then used to change the student's age to 21, andget_age
is called again to retrieve and print the updated age, which now outputs21
.
By using getter and setter methods, you maintain better control over the attributes of the Student
object. The private attribute _age
is not directly accessible from outside the class, but it can be retrieved and modified through the well-defined methods get_age
and set_age
. This encapsulation helps ensure data integrity and provides a clear interface for interacting with the object's data while allowing for validation and logic to be applied as needed.
Example 7: Static Method
Static methods are defined using @staticmethod
. They don't have access to instance-specific data and can be called on the class itself, as shown in the example. Here, we define a static method add
to perform addition.
class MathUtils:
@staticmethod
def add(x, y):
return x + y
result = MathUtils.add(3, 5)
print(result) # Output: 8
In this code:
MathUtils
is a class that contains a static method calledadd
.@staticmethod
is a decorator in Python that indicates that the following method is a static method. It's not required for a method to be static, but it helps make the code more readable and explicit.
The add
method is defined to take two parameters x
and y
and simply returns their sum x + y
.
Note the following characteristics of static methods:
- No Access to Instance Data: Static methods do not have access to instance-specific data or attributes. This means they cannot access attributes like
_name
or_age
if they were defined in the class. They work with the arguments and any other data passed to them explicitly. - Called on the Class: You can call a static method on the class itself, without creating an instance of the class. In your example, you call
MathUtils.add(3, 5)
directly on theMathUtils
class. - No
self
Parameter: Unlike instance methods, which have aself
parameter as the first argument, static methods do not have a special parameter for the instance (i.e., noself
parameter). They operate solely on their input arguments.
In this example, MathUtils.add(3, 5)
calls the static add
method, passing in the values 3
and 5
. The method performs the addition and returns the result, which is stored in the result
variable and printed, resulting in an output of 8
. Since static methods are not tied to any specific instance, they are useful for utility functions or operations that do not depend on the state of an object but are related to the class itself.
Additional Blogs by Author
- Python Function: Type of Arguments in a Function
2. Understanding Python’s init Method: Object Initialization in Depth
3. Python’s main: Setting the Stage for Your Code
4. Understanding Python’s Try-Except Statements: A Safety Net for Your Code
5. Python OOP Concepts Made Simple
6. Lambda Functions in Python
7. Python Pandas: Creative Data Manipulation and Analysis