Thursday 19 July 2018

Python Static Methods

Cited from the book "Python How to Program"

In Python 2.2 all classes (not only classes that inherit from object) can define static methods. A static method can be called by a client of the class, even if no objects of the class exist. Typically, a static method is a utility method of a class that does not require an object of the class to execute.

A class designates a method as
static by passing the method’s name to built-in function staticmethod and binding a
name to the value returned from the function call. Static methods differ from regular
methods because, when a program calls a static method, Python does not pass the object-
reference argument to the method. Therefore, a static method does not specify self as
the first argument. This allows a static method to be called even if no objects of the class
exist.

Static methods can be called either by using the class name in which the method is
defined or by using the name of an object of that class. Function main (lines 36–58) dem-

When a method of a class does not require an object of the class to perform its task,
the programmer designates that method as static.





No comments:

Post a Comment