Home >>Python Tutorial >Python Classes and Objects

Python Classes and Objects

The Python Classes and Objects

This is the Python classes and objects part of our entire Python tutorial. And we have said it before even in the introduction that this programming language is object-oriented. This means that almost everything that is present in this programming language is a particular object that has its own methods and properties that it follows in this programming language. Therefore, a class can be imagined as a type of object constructor or a kind of blueprint that is required to create or build any particular object.

To Create a Class - The Python Classes and Objects

If you wish to create a class then you only have to use the class keyword. For example, if you wish to create a class named MyClass that has a particular property of x then
class MyClass :

   x = 5
 

To Create an Object - The Python Classes and Objects

As a developer, if you further wish to create a Python object class named p1 in the class named MyClass and then if you wish to print the particular value of x then you just need to follow the method given below.
P1 = MyClass ( )

print (p1.x)
 

The _int_() Function

The Python object class example that we have mentioned above is just for your basic understand because of the fact that those examples are extremely simple and they do not hold any kind of significance when it comes to the real-life scenarios and applications. However, if you wish to understand the entire concept of the class in this programming language then it is also important for you to understand the function of _int_. This is a built-in function. It is important for you to know that every single class in this programming language has a particular function of _int_ present. This function is executed every single time when the class is initiated. It is recommended that if you need to assign values to a number of different object properties then you should use this _int_ function once the entire object is created. If you want a create a particular class named Person then you can use the _int_ function to assign the values for age and the name then the Python class example code for that is mentioned below.
class Person :

   def _init_(self, name, age) :

       self.name = name

       self.age = age



p1 = Person (“John”, 36)



print (p1.name)

print (p1.age)
 

The Object Methods

It is important for you to remember that objects can contain a number of different methods too. Any method that is used in any particular object is basically a function that belongs to that object. To understand this in a better way let us assume that you need to create a particular method in the Person class. So, let us assume that you want to insert a particular function that prints greetings and then you wish to execute this on the p1 object then the Python class example code for this is mentioned below
class Person :

   def _int_(self, name, age) :

      self.name = name

      self.age = age



   def myfunc(self) :

      print (“Hello my name is ” + self.name)



p1 = Person(“John”, 36)

p1.myfunc ( )
  It is important for you to understand that in the above example the self parameter is actually a reference to the class itself. This parameter is used to access all kinds of variables that belong to this particular class. The Self Parameter As we mentioned above, that this parameter is a reference to the Python object class in itself and it is also used to a number of other variables that belong to the same class. However, you do not have to name the parameter self you can name it whatever you like or wish. But it is important for you to understand that the parameter needs to be the first parameter of that particular class. For example, if you wish to use the words mysillyobject and abc in place of self then
class Person:

def _init_ (mysillyobject, name, age) :

   mysillyobject.name = name

   mysillyobject.age = age



   def myfunc (abc) :

     print (“Hello my name is ” + abc.name)



p1 = Person (“John”, 36)

p1.myfunc( )
 

To Modify Object Properties

You can follow the below-mentioned Python class example code to modify the various properties on an object by setting the age of p1 to 40.
p1.age = 40
 

To Delete Object Properties

You can also choose to delete the properties of an object by using the del keyword. For example, if you wish to delete the age property from the p1 object then
del p1.age
 

To Delete Objects

It is important for you to also be aware of the fact that you can also delete objects by using the del keyword. For example, if you wish to delete the p1 object then
del p1
With this, we finish the Python classes and objects part of our entire Python tutorial.

No Sidebar ads