0

I am writing a program similar to the short program below:

class Class():
 def __init__(self):
  pass
 def foo():
  pass

Whenever I call foo as Class.foo() everything inside foo works fine.

However, when I call it as Class().foo(), then I get an error: by calling Class I give foo an extra argument, self.
If I added the argument self to foo, then it won't work if I call the function as Class.foo().

How can I avoid this? Any help would be greatly appreciated :)

Arale
  • 39
  • 1
  • 8
  • 2
    Which programming language is that? Please add an appropriate tag. Also, if you know that you can call that method the one way, but not the other, why not simply use the working way? – Nico Haase Apr 24 '21 at 18:05
  • @NicoHaase its python sorry i added the tag – Arale Apr 24 '21 at 19:00
  • Seems to be related: [Static methods in Python?](https://stackoverflow.com/q/735975/45183410) – wjandrea Apr 24 '21 at 23:18

1 Answers1

1

It looks like foo should be a staticmethod.

class Class:
    @staticmethod
    def foo():
        pass

print(Class.foo())  # -> None
print(Class().foo())  # -> None

P.S. I can expand on this if needed. The question is a bit vague.

wjandrea
  • 28,235
  • 9
  • 60
  • 81