Python language gained a lot of popularity among developers for its easiness to use and companies because it is used for almost every industry you need to develop. The support of object-oriented programming increases this language’s productivity. These characteristic makes it less stressful to create a system and reduce development time.
Object-oriented is a type of programming where you break your code into pieces using the idea of objects. It is an excellent way to develop software that doesn’t need to make the same code inside the program.
1. Classes
Consider the python class as an outline for creating an object. You can say that the object is anything you want to change or use in your code, and objects handle functions and data elements. The class defines the methods used on the object to change its state. Let me show you an example:
#Creating the class
class Manager:
#Initaiting the method
def __init__(self, name, salary):
#The instance attributes
self.name = name
self.salary = salary
#Creating the object
ma1 = Manager("John", "7000")
#Printing the object name & salary
print(ma1.name, ma1.salary)
Output:
John 7000
Next, we create a function inside the class using the keyword def, and we use something called reserved method __init__, which will be initiated whenever an object is created for this class. If you don’t know how to use the functions in python then I recommend seeing this Functions In Python article.
This __init__ method can hold many values, and the first one is self which is always used when creating a function inside the class and followed by the parameters we want to use for this class Manager, which is its name and salary:
def __init__(self, name, salary):
Every object has an attribute, so you need to define these attributes inside this __init__ function as you see:
self.name = name
self.salary = salary
Here we’ve ended up creating a very simple class. Now we need to create an instance (object) for that class. We will use this command:
ma1 = Manager("John", "7000")
You can see that we’ve first created a variable called ma1, and we call the class as we do in the function, followed by the name and salary of this object we’ve created.
We add the name and salary between the closed brackets because we’ve defined that in the __init__ function, we need a name and salary for every object you create. You can access these values for every object you create using this command:
print(ma1.name, ma1.salary)
2. Add Many Objects
In the previous example, we’ve only created one object of that class, but you can make as much as you need from this class:
ma1 = Manager("John", "7000")
ma2 = Manager("Dana", "9000")
print(ma1.name, ma1.salary)
print(ma2.name, ma2.salary)
Let’s say that you want to create many objects from one class. Then it would be better practice to write them like this example:
manager = [Manager("John", "7000"), Manager("Dana", "9000")]
print(manager[0].name, manager[0].salary)
print(manager[1].name, manager[1].salary)
3. Class Attributes
Remember that these instance attributes belong only to the object you create but not the whole Manager class. Let me show you an example:
#Creating the class
class Manager:
#Class Attribute
class_att = "Experienced Manager"
#Initaiting the method
def __init__(self, name, salary):
#The instance attributes
self.name = name
self.salary = salary
#Creating the object
ma1 = Manager("John", "7000")
#Printing the object name & salary
print(ma1.name, ma1.salary)
#Printing the class attribute
print(Manager.class_att)
Output:
John 7000
Experienced Manager
You can see that we could specify an attribute outside the __init__ function, and it belongs to the class you can print or use later.
4. Class Functions
Python classes can accept adding functions as much as you need. These functions can access these attributes and use them, and these functions can take parameters. Let me show you an example:
#Creating the class
class Manager:
#Class Attribute
class_att = "Experienced Manager"
#Initaiting the method
def __init__(self, name, salary):
#The instance attributes
self.name = name
self.salary = salary
#Adding a functions
def managing(self):
print("{} is responsibe for the company activities".format(self.name))
#Creating the object
ma1 = Manager("John", "7000")
#Printing the managing function
ma1.managing()
Output:
John is responsibe for the company activities
First, you need to create a function inside the class using the def keyword followed by the function name, which is managed in the above example.
You will need to use the term self in every function you create inside this class, and these functions can access the class attributes:
def managing(self):
print("{} is responsibe for the company activities".format(self.name))
When you create a function inside the class you can use it later in your code outside that class. We call it by using the object name followed by the function name that belongs to the created from that object:
ma1.managing()
5. Function Arguments
This previous function has used only the attributes of this class, but what if you want to use another thing in the function that is not from the class attribute. Let’s see how you can achieve this:
#Creating the class
class Manager:
#Class Attribute
class_att = "Experienced Manager"
#Initaiting the method
def __init__(self, name, salary):
#The instance attributes
self.name = name
self.salary = salary
#Adding a functions
def managing(self):
print("{} is responsibe for the company activities".format(self.name))
def work(self, work_type):
print("{} is {} the company team".format(self.name, work_type))
#Creating the object
ma1 = Manager("John", "7000")
#Printing the managing function
ma1.managing()
#Printing the work function
ma1.work("organizing")
Output:
John is responsibe for the company activities
John is organizing the company team
The new function is called work(), and it has the same parameters as the previous one, such as the self keyword, and it takes the manager’s name from the __init__ function. But we’ve added another parameter which is work_type:
def work(self, work_type):
print("{} is {} the company team".format(self.name, work_type))
When you call the function later to use it, make sure to add the parameter value of the work_type, which is organizing in this example:
ma1.work("organizing")
6. Return Information
You can make a simple function inside the class to return some information about a specific object:
#Creating the class
class Manager:
#Class Attribute
class_att = "Experienced Manager"
#Initaiting the method
def __init__(self, name, salary):
#The instance attributes
self.name = name
self.salary = salary
#Adding a functions
def managing(self):
print("{} is responsibe for the company activities".format(self.name))
def work(self, work_type):
print("{} is {} the company team".format(self.name, work_type))
def manager_info(self):
info = f"name = {self.name}, salary = {self.salary}"
return info
#Creating the object
ma1 = Manager("John", "7000")
#Printing the managing function
ma1.managing()
#Printing the work function
ma1.work("organizing")
#Returning information about the object
ma1.manager_info()
Output:
John is responsibe for the company activities
John is organizing the company team
'name = John, salary = 7000'
We’ve created another function called manager_info() that will return some information about the object you create, which is its name and salary:
def manager_info(self):
info = f"name = {self.name}, salary = {self.salary}"
return info
We use this return keyword to tell the function which data you will print when we call this function. You later call it using this command:
ma1.manager_info()
Feel free to create as many functions as you want to return any information about a specific object.
Conclusion
Thanks for reading! This article was a straightforward one about how to work with classes in python, which is known as object-oriented programming. Later, we will have another part to explain more about this kind of programming.