Python Class and Object | How to use class and object in python

Python Class and Object 

python class and object


Python is an object-oriented programming language. Unlike procedure-oriented programming, where the main emphasis is on functions, object-oriented programming stress on objects. 

The object is simply a collection of variables and methods (functions) that act on those data. And, class is a blueprint for the object. 

We can think of a class as a sketch of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.


Class 

A class is a blueprint for any functional entity which defines its properties and its functions. Classes are a user-defined data type that is used to encapsulate data and associated functions together. It also helps in binding data together into a single unit. Like Human Being, having body parts and performing various actions.

In Python, a class is defined by using a keyword class like a function definition begins with the keyword def.


Class Syntax 

class ClassName:
    Statements

The Statements consists of all the component statements defining class members, data attributes, and functions.

Example:

class abc:
    a=20
    print (a)

Output:

20


Object 

The object is an entity that has a state and behavior. It may be anything. It may be physical and logical. We can create new object instances of the classes. The procedure to create an object is similar to a function call.


Object Syntax

obj=class_name()

This will create a new instance object named obj. We can access attributes of objects using the object name prefix. 

Attributes may be data or methods. The method of an object is the corresponding functions of that class. Any function object that is a class attribute defines a method for objects of that class.

Example:

class abc:
    def fun(self):
        print ("Class and object")
obj=abc()
obj.fun()

Output:

Class and object

You may have noticed the self parameter in function definition inside the class but, we called the method simply as obj. fun() without any arguments. This is because, whenever an object calls its method, the object itself is passed as the first argument.


self Parameter 

The self parameter is a reference to the class itself and is used to access variables that belong to the class. 

It does not have to be named self, you can call it whatever you like, but it has to be the first parameter of any function in the class.


The __init__() Function 

To understand the meaning of classes we have to understand the built-in __init__() function. 

All classes have a function called __init__(), which is always executed when the class is being initiated. 

Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the the object is being created.

Example:

class abc:
    def __init__(self,a,b):
        self.a=a
        self.b=b

    def fun(self):
        print ("Value of A is"self.a,"and B is"self.b)
obj=abc(10,20)
obj.fun()

Output:

Value of A is 10 and B is 20


Example:

class abc:
    def __init__(self,a,b):
        self.a=a
        self.b=b

    def fun(self):
        print ("Value of A is"self.a,"and B is"self.b)
obj=abc(10,20)
obj.fun()
obj=abc(100,200)
obj.fun()

Output:

Value of A is 10 and B is 20
Value of A is 100 and B is 200

Next Post Previous Post
No Comment
Add Comment
comment url