@classmethod vs. @staticmethod

333 views
Skip to first unread message

Sergiu Ivanov

unread,
Aug 28, 2012, 9:27:30 AM8/28/12
to sy...@googlegroups.com
Hello,

While writing one of my classes, I needed to factor out some bits of
functionality into private, instance-independent functions. To avoid
supplying any extra arguments to these functions, I have made them
into static methods using the @staticmethod decorator.

However, Tom has pointed out [0] that @classmethod would be preferable
instead. I have tried to google why that would be the case, but I
haven't managed to dig anything out. Grepping over the source of
SymPy reveals the usage of @staticmethod on occasions almost as
numerous as those where @classmethod is used.

Is there a fixed strategy when to use one of the two decorators in
SymPy?

Sergiu

[0] https://github.com/scolobb/sympy/commit/603fd9e97f5b3215f3165f265a806b09fb80a830#sympy-categories-diagram_drawing-py-P6

Tom Bachmann

unread,
Aug 28, 2012, 12:17:07 PM8/28/12
to sy...@googlegroups.com
I'm afraid I cannot find anything real either, so maybe it's not really
true. I remember this from a talk by guido I watched a while ago.

Some vague support of my memory:

http://mail.python.org/pipermail/tutor/2010-September/078542.html

http://www.peterbe.com/plog/newfound-love-of-staticmethod [first comment]

http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python

Mateusz Paprocki

unread,
Aug 28, 2012, 1:27:23 PM8/28/12
to sy...@googlegroups.com
Hi,

On 28 August 2012 15:27, Sergiu Ivanov <unlimite...@gmail.com> wrote:
Hello,

While writing one of my classes, I needed to factor out some bits of
functionality into private, instance-independent functions.  To avoid
supplying any extra arguments to these functions, I have made them
into static methods using the @staticmethod decorator.

However, Tom has pointed out [0] that @classmethod would be preferable
instead.  I have tried to google why that would be the case, but I
haven't managed to dig anything out.  Grepping over the source of
SymPy reveals the usage of @staticmethod on occasions almost as
numerous as those where @classmethod is used.

Is there a fixed strategy when to use one of the two decorators in
SymPy?

classmethod decorator gives you the class as the first argument (cls) to a method. Besides this it works the same as staticmethod. If you need the class (e.g. to access other static methods/variables) then use classmethod. Otherwise use staticmethod. Use `git grep -A 1 @classmethod` to see what method signatures follow this decorator.
 

Sergiu

[0] https://github.com/scolobb/sympy/commit/603fd9e97f5b3215f3165f265a806b09fb80a830#sympy-categories-diagram_drawing-py-P6

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.


Mateusz

Aaron Meurer

unread,
Aug 28, 2012, 1:31:49 PM8/28/12
to sy...@googlegroups.com
The difference comes mainly when subclassing. You can think of a
staticmethod as just a function definition inside a class. It has no
self argument, so it basically ignores the class it is defined in. It
is just like having the function outside the class. It's use is to put
it closer to the code that uses it for stylistic reasons.

A classmethod on the other hand does have self. So you should use this
if the class is important. Remember that in Python self always refers
to the class the method is being called on. So if you have class A
with a classmethod and subclass B of A, and you call the classmethod
on B, then self will refer to B.

A classmethod is usually used for alternate initializers, one of the
prime examples of something you want to call on a class, not an
instance.

Aaron Meurer

Aaron Meurer

unread,
Aug 28, 2012, 1:32:57 PM8/28/12
to sy...@googlegroups.com
On Aug 28, 2012, at 11:27 AM, Mateusz Paprocki <mat...@gmail.com> wrote:

Hi,

On 28 August 2012 15:27, Sergiu Ivanov <unlimite...@gmail.com> wrote:
Hello,

While writing one of my classes, I needed to factor out some bits of
functionality into private, instance-independent functions.  To avoid
supplying any extra arguments to these functions, I have made them
into static methods using the @staticmethod decorator.

However, Tom has pointed out [0] that @classmethod would be preferable
instead.  I have tried to google why that would be the case, but I
haven't managed to dig anything out.  Grepping over the source of
SymPy reveals the usage of @staticmethod on occasions almost as
numerous as those where @classmethod is used.

Is there a fixed strategy when to use one of the two decorators in
SymPy?

classmethod decorator gives you the class as the first argument (cls) to a method. Besides this it works the same as staticmethod. If you need the class (e.g. to access other static methods/variables) then use classmethod. Otherwise use staticmethod. Use `git grep -A 1 @classmethod` to see what method signatures follow this decorator.

Oh good point that I forgot. You should use cls instead of self as the first argument to a classmethod. 

Aaron Meurer

Sergiu Ivanov

unread,
Aug 29, 2012, 11:07:24 AM8/29/12
to sy...@googlegroups.com
Thank you, Tom, Mateusz, and Aaron! Apparently, my own understating
of these two decorators was quite all right. The functions I wrote
are really independent of the class and of the instance, but they do
belong logically to the class, because they eventually serve to
produce the final result the class holds and make little sense
somewhere else.

On Tue, Aug 28, 2012 at 7:17 PM, Tom Bachmann <e_m...@web.de> wrote:
>
> I'm afraid I cannot find anything real either, so maybe it's not really
> true. I remember this from a talk by guido I watched a while ago.
>
> Some vague support of my memory:
>
> http://mail.python.org/pipermail/tutor/2010-September/078542.html
>
> http://www.peterbe.com/plog/newfound-love-of-staticmethod [first comment]
>
> http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python

Well, that first comment under the post linked to by the second link
sounds rather solid. I have a very vague apprehension of why static
methods could be bad, but, on the other hand, a lot of code uses them.

Tom, do you think I should rewrite my code to use @classmethod instead
of @staticmethod? I really don't have a strong opinion on this since
I'm not a Python expert, and rewriting the methods should be pretty
easy.

Sergiu

Tom Bachmann

unread,
Aug 29, 2012, 2:01:22 PM8/29/12
to sy...@googlegroups.com
No. Nobody seems to have a strong opinion, so let's not do unnecessary
changes.

Sergiu Ivanov

unread,
Aug 29, 2012, 2:04:27 PM8/29/12
to sy...@googlegroups.com
Great! Thank you for the feedback!

Sergiu
Reply all
Reply to author
Forward
0 new messages