[Tutor] what do you use @staticmethod for?
alan.gauld at btinternet.com
Thu Nov 20 01:04:44 CET 2008
"spir" <denis.spir at free.fr> wrote
> I have not yet found any use for this feature.
While there are subtle differences I believe the biggest
reason for both being present is history. static methods
were there first then class methods were added soon after
and are slightly more flexible. But statics were kept
because people were already using them.
Personally I usually use classmethod nowadays.
> Also, I do not really understand the difference with @classmethod,
> from the programmer's points of view (even if I get the difference
> on the python side).
> As I see it, a classmethod is a very ordinary method, except its
> 'owner' is a type.
Which makes it very different to an instance method. instance
methods act on instances. class methods act on the entire
class - ie they can affect all of the instances or none.
You don't need to use class methods(or statics) very often
but when you do they are invaluable. The example of a factory
method, or a selection method (from a database say), or a cache
of instances. All of these can be done elegantly with class methods.
Author of the Learn to Program web site
More information about the Tutor