Python Programming – Class Example

In this Page, We are Providing Python Programming – Class Example. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Class Example

Class example

Till now, some basic concepts of class has been discussed. The following example “ClassExample.py” defines’a class Person, which handles name and age of multiple individuals.

class Person: 
          " " "The program handles individual's data" " "
population=0

def ___init____ ( self , Name , Age ) : 
           " " "Initializes the data." " "
          self.name=Name
          self.age=Age
          Person.population+=1

def ___del___ ( self ) : 
         " " "Deleting the data." " "
        print ( ' Record of {0} is being removed'.format(self.name)) Person.population-=1

def AgeDetails ( self ) :
         ' ' 'Age details : ' ' ' 
       print ( ' { 0 } is { 1 } years old ' . format ( self . name , self . age ) )

def Records ( cls) : 
           " " "Print number of records." " "
          print ( ' There are {0} records ' . format ( cls.population ) )

records=classmethod ( Records )

print Person . ___doc___ 
record1=Person ( ' Ram ' , 26 )
print Person.AgeDetails. ___doc___ 
record1 . AgeDetails ( )
Person.records ( ) 
record2-Person ( ' Ahmed ' , 20 )
print record2 . AgeDetails. ___doc___ 
record2 . AgeDetails ( ) 
record2 . records ( ) 
record3=Person ( ' John ' , 22 )
print Person . AgeDetails. ___doc___ 
record3 . AgeDetails ( )
Person . records ( ) 
del recordl,record2 
Person . records ( )

The output is:

The program handles individual's data 
Age details :
Ram is 26 years old 
There are 1 records 
Age details :
Ahmed is 20 years old 
There are 2 records 
Age details : 
John ih 22 years old
There are 3 records 
Record of Ram is being removed 
Record of Ahmed is being removed 
There are 1 records

Variables defined in the class definition are class variables (population is a class variable); they are shared by all instances. To create instance variables (name and age are instance variables), they can be initialized in a method, e.g. self. name=value. Both class and instance variables are accessible through the notation self. name and an instance variable hide a class variable with the same name when accessed in this way. Therefore, the class variable population is better referred to as Person. population, and not-self. population. The instance variables name and age are referred to as self. name and self. age, respectively.

The Records is a method that belongs to the class and not to the instance. This is done by using classmethod ( ) built-in function. A class method receives the class as an implicit first argument, just like an instance method receives the instance. The class method can be called either on the class (Person. records ( )) or on an instance (record2 . records ( )). The instance is ignored except for its class.

The ___doc___ attribute is used to access docstrings of class (Person. ___doc___ ) and methods (record2 . AgeDetails . __doc___).

The ___del___ ( ) method is called when an instance is about to be destroyed. This is also called a destructor.