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

Mac Ryan quasipedia at gmail.com
Fri Aug 28 13:47:09 CEST 2009


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.



More information about the Tutor mailing list