8

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.

user1836155
  • 858
  • 14
  • 29
  • 1
    Have you read [*THIS*](http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python)? – Anzel Apr 13 '15 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. – Anzel Apr 13 '15 at 21:24
  • No worries. I understand you are trying to be helpful, although the caps did trigger my adrenaline a bit. – user1836155 Apr 13 '15 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. – Anzel Apr 13 '15 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. – user1836155 Apr 13 '15 at 22:05
  • Interesting, for general console applications I'm actually with @chepner, I hardly remember to have coded any `classmethod` at all. But for webapps it almost certainly has `classmethod` on models just like @erewok's comment. For desktop you mean GUI? – Anzel Apr 13 '15 at 22:11
  • No, it's pure back-end logic. No UI – user1836155 Apr 14 '15 at 14:48

1 Answers1

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • I think I might disagree with this, but the examples the leap to mind all originate in Django, where I find myself declaring `classmethod`s 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 `staticmethod`s 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. – erewok Apr 13 '15 at 21:05
  • I should probably change "rarely need" to "unless you need". – chepner Apr 13 '15 at 21:10
  • Hmm, that's the opposite to what I expect. I am glad I asked. – user1836155 Apr 13 '15 at 21:28
  • After the edit, I think I agree more with this answer. I don't personally see much benefit to `staticmethod`s, 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'. – erewok Apr 13 '15 at 23:29