Python-Classes and objects

     Object Orientated programming :   


      In the previous section , we had discuss about Classes in Python .Definition
and Syntax of Class and Object . We saw the __init__() which initializes an object
when it is created .Similar to this __init__() method we have one more special method.
The __del__()Method which has exactly the opposite work from __init__() method .




Python-Classes and objects



The __del__() Method :

     The __del__() Method is called automatically when an object is going out of scope .
This is the time when an object will no longer be used and its occupied resources are returned
 back to the system so that they can be reused as and when required. Users can do this by calling __del__() Method explicitly.



  NOTE
  The "__del__()" Method is similar to "destructor" in c++ and Java .

Example :


class A:
   def __init__(self):
       print("Hello")
   def __del__(self):
       print("The __del__() Method called ")

OBJ = A()
del OBJ

OUTPUT :
Hello
The __del__() Method is called

Thus , we can see that the _del_() is invoked(called) when scope of object is about to end.
This method might be used to clean up any resources used by it .



There are some other SPECIAL METHODS in Python
1)__repr__() : This method has built in function with syntax repr (obj) .It will return a string
representation of an object . The function works on any object .
2)__cmp__() : This method is used to compare two class Objects. __cmp__() method can
bedefine to write comparison logic between two objects .
3)__len__() : This method function has a built in function len(obj). It returns the length of an
object .


Public and Private data Members :

Public Variables : Public variables are those variables that are define the class and can be
accessed from anywhere in the program ,With the help of (.) dot operator . anywhere means
We can access class members inside as well as outside of the class .

Private Variables : Private variables are those variables that are defined in the class. And
can be accessed inside only that particular class. Syntax to define a private member in
class with a double score prefix ( __ ).

  NOTE
  All members in python are public by default . unlike in C++ and Java where variables are private by default.

EXAMPLE:



class student :
   def __init__(self,rollno):
      self.rollno=rollno
      __div = "A"
obj =student(1123)
print("Roll number of student",obj.rollno)
print("Division of student", __div)




In above example we have create one private variable with double score (__) syntax ,
i.e. __div , which can be access inside a class , but If user try to access __div outside class
,compiler pass an error . on similar ground we have created one another variable i.e. rollno
which is public by default . Have access inside and outside class as well .




Calling Private Attributes Outside class :


   As a good Programmer we should not try to access private attributes (Variables) .But
because of some reason if user want to access private attribute .That is also possible with
the help of following syntax

objectname._classname__privatevariablename

EXAMPLE :


Private Methods :

In python there is another important concept i.e. Private method in a class. Usually
a programmer keep those method private which contain implementation details .So like
private attributes user can't be able to use private method outside the class .but because of
some reason If user want to access a method outside a class , then it can be done by a special
syntax

objectname._classname__privatemethodname

EXAMPLE:



In the above example we have access to a private method outside class . But It is possible
only with a special syntax . otherwise the compiler will pass an error for a private method .



Important point to Remember :
Python allows user to have private methods to
discourage people from accessing parts of
Program that have implementation details .



                                              Thank u 😊....