PYTHON CLASS AND OBJECTS

In this article, you will learn about Python object and class. You will also learn how to create  and use it in the main program with objects.

 

python class and object

reference; tech.geekboots.com

Python object and class

Python is an object oriented programming language. In Python,  we  treat functions, modules, data types etc object .

A class is simply a blueprint of a data that defines the characteristics and behavior of its data members and member functions and an object is an instance of the class. therefore ,Creating an object is like defining a variable of class type.

In Object Oriented Programming (OOP) we use objects to store data and functions define in a class.

When we define a class we define just a blueprint or we just map data members and member functions. A class do not occupy any physical space in computer memory until an object of that class type is define. In addition ,The declaration of class develops a template but data members cannot be manipulate unless the object of its type is create.

How to define a class

The definition of class in Python starts with the keyword class followed by the name of the class and colon

(:)

.

therefore , After we declare a class ,  body of class  have variables, data, and function. in addition ,  data, variables, and functions are bind  in a class and this bind is  Encapsulation.

Example

Let’s define a simple class of cars.

class car:

  "A simple class for cars"

    

def __init__(self,company,color):

          

    self.company = company
self.color = color

    

  def display(self):

       print ('This is a', self.color, self.company)

First is class declaration after it is  follow by documentation string. The

docstring

can be access using

class_ name.__doc__

 i.e. 

car.__doc__

.

After doc string is the

__init__

 method.

__init__ method

__init__

method is use for the initialization of the data members. This serves as the constructor for the class and as any function, it can take any number of arguments.

The 

__init__function

is automatically invoke when we create object of the class .

After 

__init__

method is another function called display to print the car company and color as supply to the function.

self variable

Well in every method inside a Python class, we use

  self

as reference to the object of the class. we include  it in a function as a first parameter. When we define function  inside a class is invoke,

self

 refers to the object of the class.

We do not need to pass 

self

as argument while calling the function. Python will automatically include it when the function is call.

creating object in Python

a class is a blueprint which do not occupy any memory we  instantiate it . object is instance of class .

Creating object is same as declaring a variable of class type and we do it simply by calling the class and assigning it to an object (variable).

example :

 
>>> #creating an object obj
>>> obj = car('Ferrari','Red')
>>> #calling the function display of class
>>> obj.display()
This is a Red Ferrari

Note: When we create an object, the 

__init__

 function inside the class is automatically invoked. So, we must supply arguments as the parameters in that function.

How to delete objects in Python?

Python objects are automatically garbage collected when the last reference to the object is gone. This happens automatically.

if you want to destroy the object(which you don’t need to obviously), you can use 

del

statement to delete the object.

>>>#define an object first for the class car
>>>obj = car('BMW','Black')
>>> del obj  #this will delete the object obj

Data hiding in Python

Data hiding is also one of the core features of OOP.

we can declare  data members of a class  as private so that they can only be access in a class. This feature of OOP in which we can declare variables and class members as private and hide them making unavailable beyond the class is called data hiding.

we can make data member  private in Python by using two leading underscores. When we don’t make data members private, they can be accessed outside of class.

example:

class car:
"A simple class for cars"
#Constructor to initialize
  def __init__(self,company,color):
    self.company = company
self.__color = color
 #making color private
  #function to print car company and color
    def display(self):
    print ('This is a', self.__

color, self.company)

There we have defined the class with private variables as well.therefore, what happens when we try to access the data members now.

 
>>> obj = car('BMW','Black')  #creating the object
>>> print (obj.company)  #company is not private
>>>BMW
>>> print (obj.__color)  #color is private
AttributeError: 'car' object has no attribute '__color'

Note: mangling is process of declaring the private variable . Use this process only to avoid the name clashes of variables as it has limited support.

As you can see the private data member 

__color

 is not available outside the class.

Python built-in class attributes

There are certain built-in class attributes in Python which can be accessed using a dot

(.)

 operator.

  • __dict__ : Returns a dictionary of classes namespace.
  • __doc__: Returns the class documentation string, if defined .
  • __module__ : Return the name of the module in which the class is define.
  • __name__: Return the name of the class .

example :

class car:
  "A simple class for cars"
#Constructor to initialize
  def __init__(self,company,color):
    self.company = company
    self.color = color
 #making color private
  #function to print car company and color
  def display(self):
    print ('This is a', self.color, self.company)
print ('car.__name__ = ',car.__name__)
print ('car.__doc__ = ',car.__doc__)
print ('car.__module__ = ',car.__module__)
print ('car.__dict__ = ',car.__dict__)

Output

car.__name__ =  car

car.__doc__ =  A simple class for cars

car.__module__ =  __main__ car.__dict__ =  {‘__module__’: ‘__main__’, ‘__doc__’:

‘A simple class for cars’, ‘__init__’:

<function car.__init__ at 0x02A5D0C0>,

‘display’: <function car.display at 0x02EB6E88>,

‘__dict__’: <attribute ‘__dict__’ of ‘car’ objects>,

‘__weakref__’: <attribute ‘__weakref__’ of ‘car’ objects>}

in this tutorial we have learn about class , object , how we define  , how we define object , in addition we have learn about instantiation  , and finally how to delete object .

so therefore , we have try to cover all the topics that relate class and object , and in previous tutorial we have cover basics of oop like class , object , encapsulation ,in addition data hiding , abstraction etc .

https://javatpoint.com

%d bloggers like this: