OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Calling an instance method defined without any 'self' parameter


On 10/4/2018 4:25 AM, Ibrahim Dalal wrote:
> class A:
>      def foo():
>          print 'Hello, world!'
> 
> a = A()print A.foo       # <unbound method A.foo>print a.foo       #
> <bound method A.foo of <__main__.A instance at 0x7efc462a7830>>print
> type(A.foo) # <type 'instancemethod'>
> a.foo()           # TypeError: foo() takes no arguments (1 given)
> A.foo()           # TypeError: unbound method foo() must be called
> with A instance as first argument (got nothing instead)
> 
> 
> Clearly, foo is an instance method.

It is either a buggy instance method, missing the required first 
parameter, *or* a buggy static method, missing the decorator, making it 
appear to the interpreter as an instance method even though it it not.

  I know one should use @staticmethod for
> declaring a method static. The question here is, given the above code, is
> there any way to call foo?

Fix the bug, whichever deficiency you regard as the bug.

-- 
Terry Jan Reedy