CLASSES - Complete Guide For Python Programming (2015)

Complete Guide For Python Programming (2015)

CLASSES

Class is a data structure that is used in python to define objects, which holds data values and behavioral characteristics. Classes are the entities, which are the programs of an abstraction for a problem, and instances are realizations of such objects. The term most likely originates from using classes to identify and categorize biological families of species to which specific creatures belong and can be derived into similar yet distinct subclasses. Many of these features apply to the concept of classes in programming.

In Python, class declarations are similar to the function declarations, a header line with appropriate keyword followed by a suite as its definition, as indicated below:

def functionName(args):

'function documentation string'

function_suite

class

ClassName:

'class documentation string'

class_suite

Class in python holds multiple data items, and it can also support its own set of functions, which are called methods. You may be asking what other advantages classes have over standard container types such as lists and dictionaries.

Creating Classes

Python classes are created using the class keyword. In the simple form of class declarations, the name of the class immediately follows the keyword:

class ClassName:

'class documentation string'

class_suite

class_suite consists of all the component statements, defining class members, data attributes, and functions. Classes are generally defined at the top-level of a module so that instances of a class can be created anywhere in a piece of source code where the class is defined.

Class Declaration vs. Definition

In python there is no difference in declaring and defining classes because they occur simultaneously. The definition follows the declaration and the documentation string.

Class Attributes

A class attribute is a functional element, which belongs to another object and is accessed via dotted-attribute notation. In Python, complex numbers have data attributes while lists and dictionaries have functional attributes. When you access attribute, you can also access an object that may have attributes of its own.

For example:

- sys.stdout.write('abc')

- print myModule.myClass.__doc__

- myList.extend(map(upper, open('x').readlines()))

Class attributes are linked to the classes in which they are defined, and instance objects are the most commonly used objects in OOP. Instance data attributes are the primary data attributes that are used. Class data attributes are useful only when a "static" data type is required, which does not require any instances.

Class Data Attributes

Data attributes are the variables of the class which are defined by the programmer. They can be used like any other variable when the class is created and can be updated by methods within the class. These types of attributes are better known to programmers as static members, class variables, or static data. They represent data that is tied to the class object they belong to and are independent of any class instances.

Example of using class data attributes (abc):

>>> class C:

… abc = 100

>>> print C.abc

0

>>> C.abc = C.abc + 1

>>> print C.abc

Output:

101