Class example<\/strong><\/p>\nTill 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.<\/p>\n
class Person: \r\n \" \" \"The program handles individual's data\" \" \"\r\npopulation=0\r\n\r\ndef ___init____ ( self , Name , Age ) : \r\n \" \" \"Initializes the data.\" \" \"\r\n self.name=Name\r\n self.age=Age\r\n Person.population+=1\r\n\r\ndef ___del___ ( self ) : \r\n \" \" \"Deleting the data.\" \" \"\r\n print ( ' Record of {0} is being removed'.format(self.name)) Person.population-=1\r\n\r\ndef AgeDetails ( self ) :\r\n ' ' 'Age details : ' ' ' \r\n print ( ' { 0 } is { 1 } years old ' . format ( self . name , self . age ) )\r\n\r\ndef Records ( cls) : \r\n \" \" \"Print number of records.\" \" \"\r\n print ( ' There are {0} records ' . format ( cls.population ) )\r\n\r\nrecords=classmethod ( Records )\r\n\r\nprint Person . ___doc___ \r\nrecord1=Person ( ' Ram ' , 26 )\r\nprint Person.AgeDetails. ___doc___ \r\nrecord1 . AgeDetails ( )\r\nPerson.records ( ) \r\nrecord2-Person ( ' Ahmed ' , 20 )\r\nprint record2 . AgeDetails. ___doc___ \r\nrecord2 . AgeDetails ( ) \r\nrecord2 . records ( ) \r\nrecord3=Person ( ' John ' , 22 )\r\nprint Person . AgeDetails. ___doc___ \r\nrecord3 . AgeDetails ( )\r\nPerson . records ( ) \r\ndel recordl,record2 \r\nPerson . records ( )<\/pre>\nThe output is:<\/strong><\/p>\nThe program handles individual's data \r\nAge details :\r\nRam is 26 years old \r\nThere are 1 records \r\nAge details :\r\nAhmed is 20 years old \r\nThere are 2 records \r\nAge details : \r\nJohn ih 22 years old\r\nThere are 3 records \r\nRecord of Ram is being removed \r\nRecord of Ahmed is being removed \r\nThere are 1 records<\/pre>\nVariables 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.<\/p>\n
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.<\/p>\n
The ___doc___ attribute is used to access docstrings of class (Person. ___doc___ ) and methods (record2 . AgeDetails . __doc___).<\/p>\n
The ___del___ ( ) method is called when an instance is about to be destroyed. This is also called a destructor.<\/p>\n","protected":false},"excerpt":{"rendered":"
In this Page, We are Providing Python Programming – Class Example. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. 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. …<\/p>\n
Python Programming – Class Example<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming - Class Example - Python Programs<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n