[Tutor] Declaration order of classes... why it is important?

Dave Angel davea at ieee.org
Fri Aug 28 14:55:06 CEST 2009


Mac Ryan wrote:
> On Wed, 2009-08-26 at 21:32 -0400, Dave Angel wrote:
>
>   
>> Now there are a couple of decorators that are in the standard library 
>> that everyone should know about:    classmethod() and staticmethod().  
>> They wrap a method in a new one (which ends up having the same name), 
>> such that the first argument is either eaten (staticmethod), or changed 
>> to a class (classmethod).
>>
>> Hope that was sufficient detail.
>>
>> DaveA
>>     
>
> Thank you all for your answer, I read the link posted by Kent end the
> explanations given by others, and now almost all the pieces of the
> puzzle are falling to their place.
>
> The (last?) thing still escaping my understanding is the difference
> between classmethod() and staticmethod(). I understand that both of them
> are to make sure a method will be associated with the class rather than
> with an instance of it (a bit like variables declared in a class but
> outside a method), so I assume I should use them like:
>
> class myClass(object):
>
>   @staticmethod / @classmethod
>   def method_of_class(self):
>     pass
>
>   def method_of_instances(self):
>     pass
>
> I am not sure I understood the difference between staticmethod end
> classmethod, though, even if I can guess it has to do with subclassing,
> (given classmethod go fish for the class name)... am I on the right
> track? Any hint (or full-fledged explanation!)? :)
>
> [The official documentation is a bit obscure for me as it refers to C#
> and Java, languages that I do not know.]
>
> Also, what is the advantage of using a method at class level rather than
> using it at object instance (I can imagine you can save some memory by
> not duplicating X times the same code, maybe... but what kind of designs
> require/call for use of statc/classmethods?)
>
> Thank you in advance for your help,
> Mac.
>
>
>   
I thought someone (me) had already posted examples.  The method itself 
looks different in the three cases:

class  MyClass(object):

     def my_method1(self, arg):
              ... do something with this particular object

     @classmethod
      def my_class_method(cls, arg)
               .... do something that may requires use of class, but 
that doesn't need to know the particular object

      @staticmethod
       def my_static_method(arg):
               ....  do something which isn't affected at all by what 
class it's in

self and cls parameters are named that merely by convention.  Unlike the 
"this" of C++, C#, and Java, these names have no specific meaning to the 
compiler. But convention is a very good thing, especially in this case.

So the first difference between static and class is that the method 
signature must be different.  There's an extra parameter on the latter 
that must be included.  The second difference is that a classmethod may 
call other methods of the same class, and gets the appropriate methods 
even if the class was subclassed.

The hint to play with is that any of these 3 may be called with an 
object  (obj.my_class_method()), and the last two may be called with a 
classname.

DaveA


More information about the Tutor mailing list