Python Classes and Objects

Python Classes & Objects

This is a detailed tutorial on Python Classes and Objects. Learn to do object-oriented programming in python using classes, objects and their methods.

Python Classes and Objects

Python can be used to do object-oriented programming. Any variable, a function or just anything that you can imagine in python that holds something is a python object. A python class can be defined as a data type, a structure or a pattern that can be used to create objects. In many other programming languages, classes are defined as the user-defined data types. But in python, even the in-built data types are defined with the label class. Therefore, Python classes can be defined as a blueprint.

Whenever you apply the python’s in-built function on any object, let it be a variable or a function, you might have noticed the word class.

Example. Observe the following code snippet and its output.

In the above code, we’ve defined four different python objects. You can see in the output that the type function tells the class of each of these objects.

Check Python Class Using Type() Function Example

Therefore, you can use the type() function to check the class of any python object. The above example basically illustrates the inbuilt classes. But you can also define your own classes and your own objects.

Creating Classes

Although there are a lot of inbuilt classes yet you might be wanted to create your own class. Well, you can create it by using the keyword class and that’s pretty easy. The syntax is simple. You’ve to write the keyword class followed by the class name and just after that, you’ve to type a semicolon and in the next line tabbed space, the body of the class starts. It is similar to creating a code block in for loops, while loops, if-else statements, and python functions.

Example. The following snippet of code illustrates how you can create a simple class named bio.

The class bio contains two properties, name and age and the values of these properties are assigned within the class.

Python Create A Class Example

In the output, you can see it prints __main__.bio, this is the notation that is printed when you print a class.

Creating Objects

The objects of a class can easily be created as demonstrated in the following example.

Example. I’ve first defined a class named bio and then I’m creating an object, using which I’m fetching two different values.

Python Class Creating Objects

Note. You can create any number of objects for a python class.

__init__() Function

The __init__() function is an inbuilt python function. It is always executed whenever a class is initialized. This is basically a method that can be written within a python class and it will automatically execute whenever you will create an object of the class. The __init__() function is like the class constructor method in most of the other programming languages like Java. In practical applications involving python programs that are designed in an object-oriented way, it is highly used.

Note. You must always define a parameter named self inside the parenthesis of the function __init__(), otherwise, it will give you an positional argument error. The argument self is always passed automatically whenever creating new objects of the class and it refers to the current class. It is like this variable in other object-oriented programming languages. But the difference is you need not to say this keyword as self always. You can write any variable name instead of self, it’s just that it has to be the first parameter of this function.

Python Class Init () Function Example

You can see when I created an object named obj of the class myFirstClass and the function __init__() is automatically called. That’s why the print statement written inside the function is executed. This function is usually used to assign class values.

Therefore, this function also allows the data to be passed directly to the class using the class name as a function.

self Parameter – Class Reference Variable

I’ve already mentioned that the first parameter of the function __init__ is used as the variable that refers to the current class like this keyword in many other object-oriented programming languages.

Example. The following example makes use of this function to assign values to the class variables name and age.

In function __init__(), I’ve assigned two parameters, name and age. Then I’m assigning the values referencing to the class variables using the class referencing variable self. Then I’ve created two different class objects obj and obj1, and passed different arguments for name and age to both of these. In the end, I’m simply printing the values using these objects.

Python Class Self Keyword & Values Assignment Using Objects

Object Methods

The classes can also contain functions and the functions defined inside python classes are known as object methods. To call these functions outside the class, we need to refer to it by creating an object of the class, just like we access class object properties.

Example. In the following code snippet, I’ve created a class named greetings and then I’ve created three different methods inside it.

The class methods are morning(), noon() and evening(). I’ve also defined the function __init__() here to assign the value of the class property name while creating an object by passing it as an argument.

Similar to the function __init__(), you must specify the first parameter as a variable that refers to the current class. Other than that, you can specify parameters to object methods like normal python functions.

Python Class Object Methods Example

Change Object Properties

The different properties of the objects can be changed using the objects themselves. It similar to the way you fetch the object properties like we’ve done so the above examples and the difference is just that now you’ve to assign them a value using one of the assignment operators like =.

Example. In the following example, we’re modifying the values for the two properties of the class that have some default values already set.

I’ve created a class object obj and then printing down the default values of its properties then I’ve modified the values and again printed out them to see the change.

Modify Object Properties Example

Deleting Classes and Object Properties

The properties of the class objects can easily be deleted using the keyword del.

Example. In the following example, I’ve deleted a class property named age using the del keyword.

I’ve first printed the two properties of the object of the class myFirstClass and then  I’ve used the del keyword to delete one of its properties and then again when I try to print that deleted property the python program raises an error.

Python Delete Class Properties

Similarly, you can also use the keyword del to delete the class itself as well.

Example. In the following snippet of code, we’ve deleted the class using the keyword del.

We’ve first printed the class, it prints well and then after using deleting it when we tried to print the class, it raises an error because the class do not exist anymore.

Python Deleting Class Example

Creating Empty Classes with pass Statement

You can also define an empty class with no properties and any other logics. But to make the class block valid and to avoid the Python End of the File error, you must use the pass statement inside the class block like we make use of it to create empty functions.

Example. In the following code snippet, we’ve created an empty class name justEmpty.

We’ve also printed the class to show that it is a valid Python Class and yet contains no properties and code. You can still create its objects and can also create its properties and set their values.

Python Create A Empty Class Using Pass Statement Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *