Complete Guide For Python Programming (2015)


In the example given below, MyFirstMethod method of the MyTeam class is simply a function which is defined as part of a class definition. This means that MyMethod applies only to objects or instances of MyTeam type.

For Example:

>>> class MyTeam:

def MyFirstMethod(self):


>>> myInstance = MyTeam()

>>> myInstance.MyFirstMethod()

Any call to MyFirstMethod by itself as a function fails:

>>> MyFirstMethod()

Traceback (innermost last):

File "<stdin>", line 1, in ?


NameError: MyFirstMethod

Here in the above example, NameError exception is raised because there is no such function in the global namespace. Here MyFirstMethod is a method, meaning that it belongs to the class and is not a name in the global namespace. If MyFirstMethod was defined as a function at the top-level, then our call would have succeeded. We show you below that even calling the method with the class object fails.

>>> MyTeam.MyFirstMethod()

Traceback (innermost last):

File "<stdin>", line 1, in ?


TypeError: unbound method must be called with class instance 1st argument

This TypeError exception may seem perplexing at first because you know that the method is an attribute of the class and so are wondering why there is a failure.

Static Methods

Python does not support static methods, and functions which are associated only with a class and not with any particular instances. They are either function, which help manage static class data or are global functions which have some sort of functionality related to the class, in which they are defined. Because python does not support static methods, so a standard global function is required.