[Tutor] Declaration order of classes... why it is important?
Mac Ryan
quasipedia at gmail.com
Sat Aug 29 12:58:05 CEST 2009
On Fri, 2009-08-28 at 18:03 +0100, Alan Gauld wrote:
> "Mac Ryan" <quasipedia at gmail.com> wrote
>
> > I am not sure I understood the difference between staticmethod end
> > classmethod, though, even if I can guess it has to do with subclassing,
>
> I think it is mainly historical. staticmethod came first (I think) and
> classmethod
> was an improvement a couple of versions later. I think classmethod is the
> preferred choice nowadays.
>
> > Also, what is the advantage of using a method at class level rather than
> > using it at object instance
>
> There are two common cases:
>
> 1) You want to do something at the class level rather than instance
> level - such as find all instances, or keep a count of all instances.
> Or you may have a method or variable that affects all instances
> simultaneously - perhaps activates or disables all network connections
> say.
>
> 2) You want somethig that can be done without creating an instance,
> or at least does not depend on whether any instances exist yet. This
> is very common in Java which doesn't support standalone functions
> but is not needed so much in Python because a module function is
> usually a better option.
>
> You can also use it to provide meta-class programming features
> and other tricks, but frankly thats usually being too clever for your
> own good! :-)
>
> HTH,
Thank you Alan, that totally answer my question about classmethods. :)
What is still unclear to me, is what the staticmethods are for, though:
since the reference to the object instance or to the class object are
stripped away from the call, I wonder why not to use a module function
instead.
The only difference I can think of between the two (static method and
module function) is that the namespaces they "live in" are different
(mymodule.function vs. mymodule.myclass.function)... but I suspect there
must be a better and more important design reason to have them
implemented in the core...
Many thanks in advance for your help,
Mac.
More information about the Tutor
mailing list