2

In Python 3.x what can I do with bar() that I cannot do with foo()?

class A: def foo(): print("some code") @staticmethod def bar(): print("some code") 

Note: I initially forgot to specify self as an argument to foo(), but I'm leaving the mistake there, since the answers andress that.

6
  • 1
    The definition of foo should be def foo(self): if you are planning to call it :) Commented Mar 21, 2019 at 6:27
  • this is not valid. foo will fail, since it requires a reference to self foo(self) Commented Mar 21, 2019 at 6:28
  • Please do not change the question, I have already addressed your initial version - and it is worth leaving in the answer.. Commented Mar 21, 2019 at 6:29
  • 1
    The difference is readily apparent once self is added as an argument. The function would never run without self anyways Commented Mar 21, 2019 at 6:30
  • So should I revert it to its original form with the error and leave an edit note, so some other learner can pick up on it? Commented Mar 21, 2019 at 6:34

2 Answers 2

3

a staticmethod is a method that does not require an object as its first parameter. This means it is a method useful to the class itself and all instantiations of it, rather than just instances of it (object initialized as A().

What this means in practical terms, is that Python does not implicitly send the object itself as a parameter. Your first method will break once you call it:

>>> a.foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() takes 0 positional arguments but 1 was given 

This is because Python supplies object methods with the object itself as a first parameter. Hence the ubiquitous self argument:

def foo(self): #Proper signature 

On the other hand,

A.bar() 

will work just fine, and so will

a.bar() 

The object is not supplied as a first argument. Use staticmethods for methods that are supposed to be helpful to the class and its instances, but do not require knowledge of either. Usually I use these as utility functions.

Note there is a third version, a classmethod, which is similar to a regular method in that it accepts a first parameter by default - the class of the caller. In this case the minimal signature is

@classmethod def operateOnClass(cls): 

Use this to make changes that affect all instances, such as changing class variables.

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

3 Comments

Maybe it's important to mention that python has three types of methods: instance methods, class methods and static methods.
Yes, please elaborate on that so I can get a fuller picture
@Will Done. Each is useful for something a bit different.
3

bar() can be called from an uninstantiated class object. foo() needs to be fed self as an argument, and as such can only be called from an object already declared as an instance of class A

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.