3

I just wondering which way is the most efficient (or pythonic) for accessing statically defined methods inside a method. Let me clear my question.

class ExampleClass:
    @staticmethod
    def do_something():
        pass

    @staticmethod
    def do_anohter_thing():
        pass

I want to call do_something inside of the do_anohter_thing. Should I use @classmethod or the class itself(ExampleClass)?

class ExampleClass:
    @staticmethod
    def do_something():
        pass

    @classmethod
    def do_anohter_thing(cls):
        cls.do_something()

or

class ExampleClass:
    @staticmethod
    def do_something():
        pass

    @staticmethod
    def do_anohter_thing():
        ExampleClass.do_something()
ErdoganO
  • 71
  • 4
  • 2
    Really depends on what you need in the end... The main difference is that `classmethod` will support inheritence. Meaning subclasses will call ***their own*** `do_something`. If you want all of them to call the one in `ExampleClass` use a `staticmethod` – Tomerikoo Mar 19 '21 at 12:16
  • 1
    I pondered whether to mark it as a duplicate. But some of the answers there mention when one is usually preferred over the other and there is also a reference to the python docs. If none of those answer your question, consider editing it to make more specific what your goal is. – Reti43 Mar 19 '21 at 12:16
  • Thanks @Tomerikoo. Your second answer the one that I looking for. Just another question, if I don't want to inherit from this class, does the `cls` and `ExampleClass` make any difference? If you post your opinion as answer, I will accept it. – ErdoganO Mar 19 '21 at 12:22
  • 1
    There is something problematic with what you said, that opinion based questions are off-topic so opinions shouldn't be answers. But I will be glad to post it as a comment. Of course it's your decision how to design your code. If you know that you or anyone else will ever inherit from this class, of course it's fine to use the class explicitly (might even be more readable). Personally I like to write generic code when I can and I would probably use a `classmethod` and call it `cls.do_something` even if I'm not inhereting in the moment... But again, that's just my opinion :) – Tomerikoo Mar 19 '21 at 12:24
  • I don't know why they marked as duplicate. My question is really different from it. – ErdoganO Mar 19 '21 at 12:24
  • Why? Please explain and I will reopen. Your question is kind of opinion based, and that linked explains extensively the differences between them and will help you choose accordingly. As I said before and as Reti said, you can try to make your question more focused on a specific use-case – Tomerikoo Mar 19 '21 at 12:26
  • I know the difference between staticmethod and classmethod. I didn't see your answer in the other question. – ErdoganO Mar 19 '21 at 12:28
  • I will expand on my previous comment with another reason why to use `classmethod` and not `staticmethod`. Let's assume some day in the future you decide to change the class' name. You will now need to go over manually on all the `staticmethod`s and change the class' name accordingly. Using a `classmethod` with the `cls` argument, you don't have to change a thing! – Tomerikoo Mar 19 '21 at 12:29
  • One last reason to use a `classmethod`: As the accepted answer in the link says about `staticmethod`s: *"Staticmethods are used to group functions which have some logical connection with a class to the class."*. But this is not just some logic connected to the class. It is calling another method of the class! So by definition it is a ***class*** method... – Tomerikoo Mar 19 '21 at 12:32
  • You probably missed [this answer](https://stackoverflow.com/a/36798076/6045800): "*To decide whether to use `@staticmethod` or `@classmethod` you have to look inside your method. __If your method accesses other variables/methods in your class then use `@classmethod`__. On the other hand, if your method does not touches any other parts of the class then use @staticmethod."* – Tomerikoo Mar 19 '21 at 12:37
  • I see that answer but I never read it's comments. Thanks! – ErdoganO Mar 19 '21 at 12:39

0 Answers0