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 .
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.
Example :
OUTPUT :
Hello
The __del__() Method is called
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 beaccessed 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 ( __ ).
EXAMPLE:
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 :
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 .
discourage people from accessing parts of
Program that have implementation details .
Thank u 😊....