10

Based on what I read, class methods are largely the same as static methods with a few exceptions but have the advantage of providing a class pointer.

As a result, is there really any reason to use static methods over class methods if a non-instance method is defined within a class?

Edit: Since some of you are quick to dismiss this as a duplicate to another question. This is not a question on the difference between the class and static methods. Rather, it is a question on how to decide between the two in the vast majority of cases when their functionality overlap.

Edit #2: The reason I ask is that I am refactoring some existing code from other people. Specifically, there are child classes that share the same modules as the parent and I intend to move them to separate modules. When that occurs, references to out-of-class constants within static methods need to be fixed. I can accomplish that with one of the following ways 1. Import all the constants from the parent module 2. Move all the constants to within parent class and change all the child static methods to class methods 3. Add the "ParentClass." before each reference to the constants

I personally want to do #2 because it avoids namespace contamination and this is also why I asked this question. It's a question of style mostly. Hope this provides enough context.

7
  • 1
    Have you read THIS? Commented Apr 13, 2015 at 20:57
  • I didn't cast the duplicate as I know you are trying to ask about WHEN, however as you haven't mentioned WHAT you have read, so I posted the comment just trying to help. Commented Apr 13, 2015 at 21:24
  • No worries. I understand you are trying to be helpful, although the caps did trigger my adrenaline a bit. Commented Apr 13, 2015 at 21:27
  • haha, be calm if you read my other posts or comments you know I have never been offensive. And without mentioning what you have read, I think most of us will post a similar comment to justify your "Based on what I read...". A good point from the comment by @erewok, is your code working on is webapps related or just general? I think this does make a difference. Commented Apr 13, 2015 at 21:32
  • I am working on a desktop application. My team's convention is that we use class method if we need the class pointer and static method if we don't. I am just trying to validate if there's anything missing in that standard. Commented Apr 13, 2015 at 22:05

1 Answer 1

7

Almost always, actually. You rarely need access to the class object that is passed automatically to a class method. Python class methods (which should not be compared to class methods in a language like Java) are primarily intended to be used to provide alternate constructors.

Specifically, a class method would look something like

@classmethod
def my_class_method(cls, foo):
   ...

If you never actually use cls in the body of the method, you might first change it to

@staticmethod
def my_static_method(foo):
   ...

Next, you might consider whether my_static_method actually needs to be part of the class and whether it could be a standalone function instead.

If you do use cls, then obviously it needs to be a class method.

Sign up to request clarification or add additional context in comments.

4 Comments

I think I might disagree with this, but the examples the leap to mind all originate in Django, where I find myself declaring classmethods on models all the time in order to have access to class variables and when the functionality should not be tied to an instance. On the other hand, I find staticmethods to be a weird way of saying, "here's a function I'm going to arbitrarily lump in with this other stuff." This is all opinion, of course, from someone who may not be fully qualified to comment.
I should probably change "rarely need" to "unless you need".
Hmm, that's the opposite to what I expect. I am glad I asked.
After the edit, I think I agree more with this answer. I don't personally see much benefit to staticmethods, so I would add even more emphasis to the 'consider whether ... actually needs to be part of the class.' I'm betting in a lot of cases, the answer is 'no'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.