Online Book Reader

Home Category

Learning Python - Mark Lutz [435]

By Root 1410 0
to be passed in when invoked. To designate such methods, classes call the built-in functions staticmethod and classmethod, as hinted in the earlier discussion of new-style classes. Both mark a function object as special—i.e., as requiring no instance if static and requiring a class argument if a class method. For example:

class Methods:

def imeth(self, x): # Normal instance method: passed a self

print(self, x)

def smeth(x): # Static: no instance passed

print(x)

def cmeth(cls, x): # Class: gets class, not instance

print(cls, x)

smeth = staticmethod(smeth) # Make smeth a static method

cmeth = classmethod(cmeth) # Make cmeth a class method

Notice how the last two assignments in this code simply reassign the method names smeth and cmeth. Attributes are created and changed by any assignment in a class statement, so these final assignments simply overwrite the assignments made earlier by the defs.

Technically, Python now supports three kinds of class-related methods: instance, static, and class. Moreover, Python 3.0 extends this model by also allowing simple functions in a class to serve the role of static methods without extra protocol, when called through a class.

Instance methods are the normal (and default) case that we’ve seen in this book. An instance method must always be called with an instance object. When you call it through an instance, Python passes the instance to the first (leftmost) argument automatically; when you call it through a class, you must pass along the instance manually (for simplicity, I’ve omitted some class imports in interactive sessions like this one):

>>> obj = Methods() # Make an instance

>>> obj.imeth(1) # Normal method, call through instance

<__main__.Methods object...> 1 # Becomes imeth(obj, 1)

>>> Methods.imeth(obj, 2) # Normal method, call through class

<__main__.Methods object...> 2 # Instance passed explicitly

By contrast, static methods are called without an instance argument. Unlike simple functions outside a class, their names are local to the scopes of the classes in which they are defined, and they may be looked up by inheritance. Instance-less functions can be called through a class normally in Python 3.0, but never by default in 2.6. Using the staticmethod built-in allows such methods to also be called through an instance in 3.0 and through both a class and an instance in Python 2.6 (the first of these works in 3.0 without staticmethod, but the second does not):

>>> Methods.smeth(3) # Static method, call through class

3 # No instance passed or expected

>>> obj.smeth(4) # Static method, call through instance

4 # Instance not passed

Class methods are similar, but Python automatically passes the class (not an instance) in to a class method’s first (leftmost) argument, whether it is called through a class or an instance:

>>> Methods.cmeth(5) # Class method, call through class

5 # Becomes cmeth(Methods, 5)

>>> obj.cmeth(6) # Class method, call through instance

6 # Becomes cmeth(Methods, 6)

Counting Instances with Static Methods

Now, given these built-ins, here is the static method equivalent of this section’s instance-counting example—it marks the method as special, so it will never be passed an instance automatically:

class Spam:

numInstances = 0 # Use static method for class data

def __init__(self):

Spam.numInstances += 1

def printNumInstances():

print("Number of instances:", Spam.numInstances)

printNumInstances = staticmethod(printNumInstances)

Using the static method built-in, our code now allows the self-less method to be called through the class or any instance of it, in both Python 2.6 and 3.0:

>>> a = Spam()

>>> b = Spam()

>>> c = Spam()

>>> Spam.printNumInstances() # Call as simple function

Number of instances: 3

>>> a.printNumInstances() # Instance argument not passed

Number of instances: 3

Compared to simply moving printNumInstances outside the class, as prescribed earlier, this version requires an extra staticmethod call; however, it localizes the

Return Main Page Previous Page Next Page

®Online Book Reader