Just like other programming languages, we use object-oriented programming in Python to write modular, reusable, and extensible code. In other words, OOP allows us to create organized code that is easy to maintain and modify when requirements are constantly changing. In this blog, we will discuss the basic oops concept in Python: Classes and Objects, Types of methods and Types of attributes in Python.
A class is a blueprint for an object that defines its attributes and methods.
In Python, an object is an instance of a class.
For example, we can create objects of an "Animal" class such as a "Dog," "Deer," and "Lion," which are based on the attributes and methods defined in the "Animal" class. These attributes may include characteristics such as sound, height, and type of animal (herbivore, carnivore, omnivore, etc).
class employee_details:
pass
# creating object of employee_details class
emp1 = employee_details()
emp1.name = "Mohan Kumar"
emp1.experience = "3.5 years"
print(emp1.name)
print(emp1.experience)
# output
Mohan Kumar
3.5 years
In the above example, we have used pass statement and have not defined any attributes and methods for the class. If we want to set the values manually, we can use the __init__ function to initialize the object's attributes. Let's understand the meaning of both terms in detail.
pass statement: Creating an empty class in Python is possible with the use of a statement known as "pass". It serves as a placeholder, allowing the code to move on to the next line without executing any instructions. In other words, pass statement can be handy when we are writing code that's not yet fully formed. It's often used with control flow statements, empty functions, or empty classes.
_init_ method: Each class should have a method called _init_ which executes when an object of the class is created. This method is used to assign values to object properties and perform other necessary operations, such as initialization (constructor method). The _init_ method is the first method called when an object of the class is created.
class employee_details:
def __init__(self, name, experience):
self.name = name
self.experience = experience
emp1 = employee_details("Mohan Kumar", "3.5 years")
print(emp1.name)
print(emp1.experience)
# output
Mohan Kumar
3.5 years
In this example, the "_init_" method takes three parameters: self, name, and experience. The self parameter refers to the instance of the class, in this case, the object "employeedetails". When we create a new object from this class and provide values for name and experience, these values are automatically assigned to the object through the "\init_" method. This simplifies the process of creating new objects and setting their attributes without having to manually set the values each time.
self keyword: We use "self" keyword in Python, which refers to the current instance of the class. Although a class is defined to describe the properties and behaviours of an object, each instance of the class will have its own unique set of property values. By using the self parameter, we can access and modify these instance properties.
To make a class more effective, it should have functions or methods that perform specific tasks. For example, in the following code, we have created a method called "employee_experience()" that can access "name" and "experience" attributes. We have used the "self" keyword within the method to allow it to be used with every object in Python.
class employee_details:
def __init__(self, name, experience):
self.name = name
self.experience = experience
def employee_experience(self):
return self.name + "has experience of -" + str(self.experience)
emp1 = employee_details("Mohan Kumar", "3.5 years")
print(emp1.employee_experience())
# output
Mohan Kumar has experience of - 3.5 years
Overall, in Object-oriented programming (OOP), Objects represent real-world concepts or entities within the program and have their attributes and methods for interacting with the rest of the program. Its attributes define the object's properties, and behavior is defined using methods. These methods are reusable pieces of code defined within a class and can be called or invoked at any point in the program.
Instance methods in Python are bound to a specific object instance and are used to set or get details about that object. They are the most common method used for accessing and manipulating the data associated with a specific object instance.
class Demo_Class():
def demo_instance_method(self):
return "This is instance method!"
obj = Demo_Class()
obj.demo_instance_method()
Instance methods have one default parameter, called "self," which points to an instance of the class. While you can change the name of this parameter, it would be best to stick with the convention of using "self." Any method you create inside a class is an instance method unless you specify otherwise.
Class methods are used to set or retrieve information about the class itself rather than a specific object instance. So they are bound to the class rather than an object instance and cannot access or modify specific instance data.
To define a class method, we must use the @classmethod decorator. Class methods also have a default parameter called "cls," which refers to the class itself. Using "cls" as the parameter name is common, but it is not mandatory.
class Demo_Class():
@classmethod
def demo_class_method(cls):
return "This is a class method."
# Accessing class methods with the help of a class instance.
obj = Demo_Class()
obj.demo_class_method()
# Accessing the class methods without creating a class instance.
# Or accessing the class method using the class name
Demo_Class.demo_class_method()
Class methods are useful for operations that involve the class as a whole rather than specific class instances. Note: We can not call instance methods using the class name.
In Python, a class or static variable is a variable that is shared by all instances of a class. It is defined inside the class but outside of any class methods. This means that it can be accessed using the class name or any instance of the class. Let's consider the following example code:
class Car:
# Class variable
wheels = 4
def __init__(self, make, model):
# Instance variables
self.make = make
self.model = model
# Static method
@staticmethod
def honk():
print("Honk honk!")
# Accessing class variable
print(Car.wheels) # Output: 4
# Creating instances of Car class
car1 = Car("Honda", "Civic")
car2 = Car("Toyota", "Corolla")
# Accessing instance variables
print(car1.make) # Output: Honda
print(car2.model) # Output: Corolla
# Modifying class variable
Car.wheels = 3
# Accessing class variable through instance
print(car1.wheels) # Output: 3
print(car2.wheels) # Output: 3
# Modifying class variable through instance
car1.wheels = 5
# Accessing class variable
print(Car.wheels) # Output: 3
# Accessing class variable through instance
print(car1.wheels) # Output: 5
print(car2.wheels) # Output: 3
In the above code, the Car class has a class variable wheels that is set to 4 by default. So, we can access the class variable wheels using the class name Car and modify it by assigning a new value to it.
We can also access the class variable through instances of the Car class like car1 and car2, but modifying it through instances will create a new instance variable with the same name that only exists within that instance.
In Python, an instance variable is a variable that is specific to each instance of a class. It is defined inside a class method, usually, the _init_ method, and can be accessed using the instance name. Let's consider the following example code:
class Car:
def __init__(self, make, model):
# Instance variables
self.make = make
self.model = model
self.odometer = 0
def drive(self, miles):
self.odometer = self.odometer + miles
# Creating instances of Car class
car1 = Car("Honda", "Civic")
car2 = Car("Toyota", "Corolla")
# Accessing instance variables
print(car1.make) # Output: Honda
print(car2.model) # Output: Corolla
# Modifying instance variables
car1.drive(50)
car2.drive(100)
# Accessing modified instance variables
print(car1.odometer) # Output: 50
print(car2.odometer) # Output: 100
Overall, instance variables allow us to define attributes that are unique to each instance of a class, and modify them as needed.
In Python, dir function helps us inspect an object and obtain a list of all the attributes and methods that can be called on it. This can be useful when working with a new object in Python and you want to explore what you can do with it. On another side, dir function, we can help us save time and reduce the likelihood of making errors by ensuring that we use the correct attribute or method for a given object.
For example, if we call dir function with car1 attributes in the above example, we will get list of attributes and methods that can be called on car1 object.
print(dir(car1))
# output
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'drive',
'make',
'model',
'odometer']
Above all, those with an underscore are default methods and attributes in Python. Others are user-defined methods and attributes.
Built-in-attributes: Built-in attributes are predefined attributes and functions provided by Python. For instance, dict is a built-in attribute that returns all key-value pairs of an object as a dictionary. For example:
print(car1.__dict__)
# output
{'make': 'Honda',
'model': 'Civic',
'odometer': 50}
Python has various built-in data types:
Each data type is considered a class in Python. When we define a class, such as the int class for the numbers 1, 2, and 3, we create three objects or instances of the int type. We can then use the methods associated with these classes, such as the sort() and reverse() methods of the list class.
These built-in methods provided by Python are beneficial for us as programmers, as they allow us to quickly and easily perform complex operations on our data structures without having to write the code ourselves.
list_example = [1,3,5,9,0]
# sorting the list
list_example.sort()
print(list_example)
# reversing the list
list_example.reverse()
print(list_example)
###output
[0, 1, 3, 5, 9]
[9, 5, 3, 1, 0]
We will cover other Python OOPS principles like encapsulation, abstraction, inheritance and polymorphism in a separate blog. Enjoy learning, Enjoy Python.