i

Python Programming

Python Classes/Objects

The class statement creates new class definition. The name of class immediately follows the keyword class followed by colon as follows:

class ClassName:

   Optional class documentation string

   class_suite

•  The class has documentation string which may be accessed via ClassName.__doc__.

•  class_suite consists of all component statements defining class members/data /attributes and functions.

Example

Following is the example of simple Python class:

The variable empcount is class variable whose value is shared with all instances of  class. This may be accessed as Employee.empcount from inside class / outside class.

  • The first method __init__() is special method, which is called class constructor or initialization method that Python calls when you create new instance this class.

  • We declare other class methods like normal functions exception that first argument to each method is self. Python adds self argument to the list for you, you do not need to include it when you call methods.