Classes are a fundamental part of the Python language. They provide a way to group related functionality together, and they play a central role in object-oriented programming. In this article, we’ll take a look at the attributes of a class in Python.
- Inheritance: Adoption of properties from the parent class into the child class.
- Polymorphism: Creation of many forms from one form.
- Abstraction: Displaying the necessary data and hiding unnecessary data.
- Encapsulation: Securing the info of the class.
More About Classes in General
- The classes are just an interface that holds variables and functions within themselves. These are known as Data Members and Member Functions respectively.
- To access them we need to create an object of that class. The object is the instance through which we can edit its properties.
- To give some parameters to the class itself there is a special method called a constructor. The method invokes in runtime when we form the object.
- We can access all the data members and member functions using the object only.
Creating Classes in Python
Classes in Python are created with the keyword class, followed by the name of the class. Class attributes are defined after the class name, and they are shared by all instances of the class. Individual instance attributes are defined after the class attributes, and they are unique to each instance. Method definitions are also placed after the class definition. Methods are functions that are associated with a class, and they are used to process or manipulate the data stored in instances of the class.
Let’s now define a class to understand this better.
class student: def __init__(self, name, std, roll_no): self.nm = name self.std = std self.rl_no = roll_no def getData(self): print("Student name: ", self.nm) print("Standard: ", self.std) print("Roll number: ", self.rl_no) def setData(self, name, std, roll_no): self.nm = name self.std = std self.rl_no = roll_no stud = student("Om", "4th", 9) stud.getData() print() # to print a new line in between stud_1 = student("Hari", "5th", 14) stud_1.getData()
Student name: Om Standard: 4th Roll number: 9 Student name: Hari Standard: 5th Roll number: 14
- Use the class keyword to declare a class. Then add class_name after it and give a colon to begin data insertions.
- Then call the “__init__()” method. This is the constructor method for any class in Python.
- We create a student class and then give the properties like name, standard, and roll number to it.
- Then we use the self keyword to make sure that the properties are properly bound to the class. There is no use of class declaration if we do not use the self keyword.
- There are two methods inside the class.
- The first one “getData()” retrieves the instance attributes.
- The second one “setData()” enables to change of the values of those attributes.
- Now we create two objects for this class. The first one has different parameters. They are the names and info about two students.
- These are called instance variables or instance attributes. They are unique for each object.
Accessing Class Variables Instance Attributes
You can access the attributes and methods of a class by using the dot operator (.). For example, if you want to access the attribute x of the class myClass, you would use the expression myClass.x. If you want to call the method myMethod of the class myClass, you would use the expression myClass.myMethod().
Let’s define a few instance attributes in our class for this demonstration.
Syntax to access the instance attributes:
object = class_name(parameter1 = value_!, parameter2 = value_2, .., parameter_N = value_N) object.parameter_1 object.parameter_2 . . object.parameter_N
class Rectangle: def __init__(self, length, width): self.side_1 = length self.side_2 = width def area(self): a = self.side_1*self.side_2 print("Area of the rectangle is: ", a) rect = Rectangle(45.0, 56.98) # printing the type of object print(type(rect)) # accessing the values through object print(rect.side_1) print(rect.side_2)
<class '__main__.Rectangle'> 45.0 56.98
So, in this way, we can access them.
Accessing Class Methods And Other Instance Attributes
class_name.variable_1 class_name.variable_2 . . class_name.variable_N
The simple change in this concept is that the class attributes we are talking about are the class variables. These variables can only be accessible using class_name only. They are also known as static variables. The memory does not clean it, rather after every successful run of code. It updates new values retaining the previous ones.
For example, we take the same student class and create class attributes for it:
class student: school = "Universal Public School" batch = "2020-2021" def __init__(self, name, std, roll_no): self.nm = name self.std = std self.rl_no = roll_no def getData(self): print("Student name: ", self.nm) print("Standard: ", self.std) print("Roll number: ", self.rl_no) def setData(self, name, std, roll_no): self.nm = name self.std = std self.rl_no = roll_no print("The school name is: ", student.school) print("The batch year is: ", student.batch, "\n") stud = student("Om", "4th", 9) stud.getData() print() # to print a new line in between stud_1 = student("Hari", "5th", 14) stud_1.getData()
The school name is: Universal Public School The batch year is: 2020-2021 Student name: Om Standard: 4th Roll number: 9 Student name: Hari Standard: 5th Roll number: 14
- The student class contains just two new things in the beginning. It contains the school and batch variables.
- The next code is the same as the first code. Others are just
getter() and setter()methods.
- Now in the 21st and 22nd lines of code, we are calling those variables.
- Note the difference:
- Rather than creating an object, we call them using the class name only.
- Then using the dot operator “.” the access is taken.
- Also, note that we can change their values using the equal-to “= “operator in runtime as well as calling them.
Example (during runtime):
class Employee: # class attributes COMPANY = "" BRANCH = "" def __init__(self, name, designation, ID): self.name = name self.designation = designation self.id = ID def getData(self): print(self.name) print(self.designation) print(self.id) print() def setData(self, name, desig, ID): self.name = name self.designation = desig self.id = ID def main(): Employee.COMPANY = input("Enter the company name: ") Employee.BRANCH = input("Enter the branch: ") print() print("...The employee details are...") print("The company name is: ", Employee.COMPANY) print("The company branch is at: ", Employee.BRANCH) emp_1 = Employee("Varun", "Tirpathi", 1001) emp_2 = Employee("Dhanush", "Reddy", 1002) emp_3 = Employee("Neha", "Singh", 1003) emp_1.getData() emp_2.getData() emp_3.getData() main()
Enter the company name: Microsoft Enter the branch: Bengaluru ...The employee details are... The company name is: Microsoft The company branch is at: Bengaluru Varun Tirpathi 1001 Dhanush Reddy 1002 Neha Singh 1003
- Here we have a simple Employee class. The constructor contains parameters like the name, designation, and ID of the employee.
- The next methods are the
getData() and setData().By the name, we can understand that the first method is for retrieving the data and the next is for editing the data.
- There are two attributes of this class:
main()the function takes the input for those two class attributes.
- In the last six-line, we have three objects of the Employee class.
- Then to retrieve data we call the getData() method.
So, in this way, we can say that the Attributes of a class are also called Class Variables. I hope this will be helpful and fun learning a new concept related to OOP and classes in Python. More to come on new topics. Till then, keep learning and progressing.