How to create Class in Python
In this article, we will learn to create a class in Python. We will look at the methodology, syntax, keywords, associated terms with some simple approaches, and some custom codes as well to better understand the topic of the class. Let's first have a quick look over what is a class, its types, and how it is used and defined in Python language.
What is a Class?
Python is a popular scripting language. It also supports the object-oriented programming paradigm concepts. Object-Oriented Programming in Python involves object creation through Class
, Inheritance
, Polymorphism,
and Encapsulation
. A class is like a blueprint that bundles different types of items under one roof. Python can have multiple classes in one Python script. Each class contains its own attributes, instances, and methods to maintain the state of the class. Python classes have the ability to protect the data from outside threats. Classes are accessed from an entity called objects
. It has methods called member functions
to modify the state of the class. Assume your class to be a house that contains house details like room, kitchen, doors, etc. Likewise, Class has different attributes called data members
that tell us about the class.
Class Real World Example
Let us understand a real-world example to know the importance of object-oriented Class. Assume there are 50 students in one class and the teacher wants to store, manage, and maintain marks of each subject scored by 50 students. In order to maintain this large data, classes are introduced because they combine data together and provide data organization. The class creates objects for its functioning. The class will define properties related to students like name, roll no, marks, etc. under one roof and then access the information by using objects created.
Now, let us see how classes are created, what important points should one keep in mind before creating a class, and then what another functionality class provides to us.
Creating a Class in Python
A program of a class is generally easy to read, maintain, and understand. A class, functions as a template that defines the basic characteristics of a particular object.
Important Points
- Classes are created using
class
keyword.
- A
colon (:)
is used after the class name.
- The class is made up of attributes (data) and methods (functions).
- Attributes that apply to the whole class are defined first and are called
class attributes
.
- Attributes can be accessed using the dot
(.)
operator via objects.
Let us understand the concept of the 'Dog'
class using a simple code.
Example: Creating Python Class
#class is defined using class keyword
class Dog:
#data members of class
color = "black" #attribute 1
name = "Polo" #attribute 2
#class constructor
def __init__(self):
pass
#user defined function of class
def func():
pass
We take a class and named it "Dog". We defined two attributes or two instances of the Dog class that store color
and name.
This is the simplest template of a class. Further, we defined a constructor that uses __init__
for its declaration. After this, the user can create their own function called member functions of the class and perform different operations on the attributes defined inside the class. We left these two functions empty and will learn more about them in another article.
As you can see in the above example, all the different attributes or properties of the dog are placed together as a bundle. These attributes are like class variables that are local to his class. When a class defined, a new namespace
is created and used as the local scope thus, all assignments to local variables (attributes here) go into this new namespace and then accessed further with the help of an object. We will learn more about class objects in the next article.
Conclusion
In this article, we learned to create a class in Python by using the class
keyword. We used the Dog class to better understand the topic. We learned about object-oriented programming with its importance. We learned how classes are used in our daily lives.