🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeAttributes of a Class in Python

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.
Code:
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()
Output:
Student name: Om
Standard: 4th
Roll number: 9
Student name: Hari
Standard: 5th
Roll number: 14
Explanation:
- 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
Code:
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)
Output:
<class '__main__.Rectangle'>
45.0
56.98
So, in this way, we can access them.
Accessing Class Methods And Other Instance Attributes
Syntax:
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