|
|
||
Static Methods and Class MethodIn a few cases, our class may have methods which depend on only
the argument values, or only on class variables. In this case, the
A method which doesn't use the Here's the syntax for using the
To evaluate a static method function, we simply reference the
method of the class:
Example of Static Method. Here's an example of a class which has a static method. We've
defined a deck shuffler. It doesn't have any attributes of its own.
Instead, it applies it's class Shuffler( object ): @staticmethod def shuffle( aDeck ): for i in range(len(aDeck)): card= aDeck.get( random.randrange(len(aDeck)) ) aDeck.put( i, card ) d1= Deck() Shuffler.shuffle( d1 ) Class Method. The notion of a class method is relatively specialized. A class method applies to the class itself, not an instance of the class. A class method is generally used for "introspection" on the structure or definition of the class. It is commonly defined by a superclass so that all subclasses inherit the necessary introspection capability. Generally, class methods are defined as part of sophisticated,
dynamic frameworks. For our gambling examples, however, we do have some
potential use for class methods. We might want to provide a base
|
||