Quick Introduction to Python class and object

In this article we will learn about Python class

Python class – Before moving ahead, let’s know a little about Python array

class – It means creating an object which provides methods and attributes.

A class can create its own object and every object can have a different variable storing the same or different value. A class is an object constructor and works as blueprint of a object.

Creating a Class – To create a class in Python, use class keyword.

Example 1- Use of class keyword to create a class with name and variable.

class Myfunc:
     z = 1
print(Myfunc) 

Explanation – In the above example, (Myfun) is a class name and it stored a variable name ‘z’ which stores value 1 and to get output used print() method.

Creating an object – Create an object with class name.

Example 2- Create an object by using class name (Myfunc)

class Myfunc:
     z = 1
x = Myfunc()

print(x.z) 

Explanation – In the above example, (Myfunc) is a class name and it stored a variable name z which stores value 1 in it. Variable x is stored class name (Myfunc) and created an object. To get value of variable z use print() method

The __init__() Function – The full form of__init__() is initialization. It is a built-in function of Python and uses to assign values to an object’s property. It executed automatically when the class is called or initiated.

Example 3- Create a class name and assign two property by use of __init__() function.

class Mydetails:

    def __init__(self, name, place):
        self.name = name
        self.place = place
x = Mydetails('Alex', 'NYC')

print(x.name)
print(x.place) 

Explanation – In the above example, two properties are defined by __init__() function that holds two different values according to its property name. As class function is called, __init__() function will be called automatically and will run the print() function with property value as __init__() function automatically will fill the value in properties according to variable name.

Note:  Every time whenever class will be used to create an object, then __init__() function called automatically.

The self-parameter – It stored objects in itself. It is used to access the variables that belong to the class. It is not always fixed to write name self, anything can be written in place of it, but it will always be the first parameter.

Example 4- Use any character instead of self parameter.

class Mydetails:

    def __init__(myobject, name, place):
        myobject.name = name
        myobject.place  = place
 x = Mydetails('Alex', 'NYC')

 print(x.name)
 print(x.place) 

Explanation – In the above example, two properties are defined by __init__() function that holds two different values according to its property name. As class function is called, __init__() function will be called automatically and will run the print() function with property value as __init__() function automatically will fill the value in properties according to variable name.

Change object value – An object value can be changed easily after once it created.

Example 5- Change of object named name’s value.

class Mydetails:

   def __init__(myobject, name, place):
        myobject.name = name
        myobject.place  = place
x = Mydetails('Alex', 'NYC')
x.name = 'Elisa'

 print(x.name)
 print(x.place) 

Explanation – In the above example, two properties are defined by __init__() function that holds two different values according to its property name. As class function is called, __init__() function will be called automatically and will run the print() function with property value as __init__() function automatically will fill the value in properties but as one value is changed, therefore new value will be filled according to variable name.

In other words – In the above example, first object named name’s value is ‘Alex’ which is changed in ‘Elisa’.

Delete object’s property– An object’s property can be easily deleted after once it created.

Example 6- Delete the object property name.

class Mydetails:

    def __init__(myobject, name, place):
         myobject.name = name
         myobject.place  = place
x = Mydetails('Alex', 'NYC')
del x.name

print(x.name) 

Explanation – In the above example, two properties are defined by __init__() function that holds two different values according to its property name. As class function is called, __init__() function will be called automatically and will run the print() function with property value but as output it will return an error as property value will be deleted by using ‘del’ keyword.

In other words – In this example, Python will return an error because before printing statement we used ‘del’ keyword to delete object’s property.

Delete object – An object can be easily deleted after once it created.

Example 7- Delete the object.

class Mydetails:

   def __init__(myobject, name, place):
         myobject.name = name
         myobject.place  = place
x = Mydetails('Alex', 'NYC')
del x

print(x) 

Explanation – In the above example, two properties are defined by __init__() function that holds two different values according to its property name. As class function is called, __init__() function will be called automatically and will run the print() function with property value but as ouput it will return an error as object will be deleted by using ‘del’ keyword.

In other words – In the above example, Python will give us an error because before printing statement we used del method to delete object.

The pass statement – A class can never be empty. But in case if it is necessary to leave blank then use pass keyword to avoid getting an error.

Example 8- Use pass keyword to create empty class.

class Mydetails:
     pass
print(Mydetails) 

Explanation – In the above example, as class function is called, it will execute nothing as ‘pass’ keyword is defined inside class function.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

1 thought on “Quick Introduction to Python class and object”

Leave a Comment

Stay in the loop

codingstreets