2

Very broad question, perhaps python specific.

Many IDE tools such as PyCharm suggest to add the @staticmethod decorator to functions which do not contain any reference to self. Are there ever occasions where a function must not be static even though it has no reference to self?

Oyster773
  • 375
  • 1
  • 10
  • 1
    The presence or absence of `self` doesn't make a method an instance method; *all* methods are instance methods *unless* decorated with `staticmethod` or `classmethod`. Once you've determined what type of method you have, the first argument can be assumed to be an instance or the class (or just a regular argument), regardless of its name. – chepner Nov 20 '17 at 01:34
  • @chepner Aside the loss of access to `self` when you declare a `staticmethod` in Python, are there any other dangers? Or is that the only difference between a static and an instance method. You can ignore the fact that it's now called differently etc. Surely if a method has no `self`, then that must indicate it's not a instance method, right? So why does PyCharm say it /may be/ a static method, not must be? Do we ever need instance methods without self? – Oyster773 Nov 20 '17 at 01:38
  • 1
    @OlafSzmidt PyCharm tells you it *may* be a static method because it isn't mandatory, regardless of it using `self`. It's up to you. One reason to have an instance method without `self` is if you currently aren't using instance information but think that you may want to in the future. Another thing to consider when you have a `staticmethod` is whether it makes sense to move it out of the class. – Paulo Almeida Nov 20 '17 at 01:54

0 Answers0