Python Classes explained with examples

In this article, we are going to learn Python Classes Introduction with examples. Python is an object-oriented programming language. Object-oriented languages rely on objects to implement the functionalities of a software application. Objects are defined as a collection of data and functions which implement the desired functionalities. The object is a bundle of this Data and functions that a Class contains inside it. Almost all modern languages which support Object-oriented have the concept of classes.

What are Python Classes?


In simple words, Classes are a kind of table of content of a book, which contains the details of the whole book’s content. Then each function and variable of the class contains the full implementation of the book that it wants to describe. Python Language supports Object-oriented programming and has the full support of Classes and objects. Python classes support almost all Object-Oriented Language concepts.

How to get and print Python class Attributes

How to Defining a Class in Python


  • The Definition of a Class in Python begins with a class keyword. The class keyword is followed by the name of the class, that we want to give to it. The class name is followed by a colon “:”.
  • It creates a new local namespace for this class, where all the attributes of the class are defined.
  • A class can contain many attributes in the form of data, functions, and some special attributes.

Python Program to Create a Class


When we define a class, the compiler/Interpreter creates an object of that class with the same name as the class name. By using this object, we can access the data, functions, and all attributes of this class. This object also helps us to instantiate this class with new objects if we need more objects from this class.

class Sample:
    "This is a Sample class"
    variableOne = 123

    def PrintMessage(self):
       print('Hello World')

#creating a class object and accessing class functionality

samobj = Sample()
#using object (samobj) to call  PrintMessage()
samobj.PrintMessage()

print(Sample.__doc__)

How does it work

  • Line number 1 in the above example is the start of the class definition, where we are using the class keyword and the name(Sample) of this class followed by a colon :
  • Line number 2, is called the docstring(comment), Using docstring is a good practice to add the description about the class functionality. It is not a mandatory line to add. It is just to improve the readability of the code.
  • Line number 3, is a simple data variable(variableOne) inside this class. A class can have any type of data variable.
  • Line number 4, is a function(PrintMessage) definition inside the class. It is continued to line number 5, which is the body of this function.
  • Line number 6, creating an object(samobj) of the class and accessing def PrintMessage(self) function. The function defines with self always access by using a class object.

How to create an object of Python Class


As we mentioned above, an object gets created with the class itself, which has the same name as of the class. We can use objects to access the data variables and functions of this class(Sample). Also, we can create an object of the class by using the class name. Below is a sample example of this object access.

#creating a class object and accessing class functionality

samobj = Sample()

#using object (samobj) to call  PrintMessage()

samobj.PrintMessage()

The attribute of Python Class


If you are thinking that this class contains only two attributes(Attributes are the variables and functions that belong to a class). Then you will be surprised to know that it is not true. Python classes contain some special attributes, which begin with double underscores __.

Here is an example:

#Special attributes in Python classes

print(Sample.__doc__)

This special attribute is used to access the docstring of any class. So just use the class object and invoke the doc function to get the docstring of that class.

In a class, the attributes are always public. It means they can be accessed from outside the class body. To access any attribute of a class whether it is a data variable or it is a function, we use the dot operator with the object. It is very simple and just the use of the object. the attribute can help us to access an attribute.

Python classes summary :


  • Classes are created by keyword class.
  • Attributes are the variables and functions that belong to a class.
  • An object with the same name gets created with the creation of the class.
  • Other objects can be created of a class as per program need.
  • The class has some special attributes that start with a double underscore(__).
  • Attributes are always public and can be accessed using the dot (.) operator.