Python Programming – Class

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

Python Programming – Class

Class

A class is the particular object type created by executing a class statement. Class objects are used as templates to create instance objects, which embodies the attributes: the “data attributes” and “methods”, specific to a data type. A class definition is given below:

classdef        : := " class " classname [ inheritance ] " : " suite
inheritance   : := " ( " [ expression_list ] " ) " 
classname    : := identifier

The above class definition might seem alien, it will become more clear with the progress of this Chapter. The simplest form of class definition looks like:

class ClassName: 
        <statement-1>
         .
 
        .

       .
      <statement-N>

The following example gives a glimpse of how a class is defined.

>>> class Output :
. . .      def Display ( self ) :
. . .            print ' This is a class example . '
. . . 
>>> x=Output ( ) 
>>> x . Display ( )
This is a class example.

Like function definition (def statements), the class definition (Output in the above example) must be executed before they have any effect. In practice, the statements inside a class definition will usually be function (or more specifically “method”) definitions (Display ( ) in the above example), but other statements are allowed. The function definitions inside a class normally have a peculiar form of the argument list, dictated by the calling conventions for methods (discussed later).

The creation of a class definition also creates a new namespace, and used as the local scope, thus all assignments to local variables go into this new namespace.