Calling an instance method defined without any 'self' parameter
Bob van der Poel
bob at mellowood.ca
Thu Oct 4 13:00:51 EDT 2018
On Thu, Oct 4, 2018 at 1:25 AM Ibrahim Dalal <ibrahimhusain007 at gmail.com>
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. 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?
>
> Python 2.7
>
>
> Thanks,
> --
> https://mail.python.org/mailman/listinfo/python-list
>
Use the magic of staticmethod :)
class A:
@staticmethod
def foo():
... do foo stuff
Hope this helps.
--
**** Listen to my FREE CD at http://www.mellowood.ca/music/cedars ****
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bob at mellowood.ca
WWW: http://www.mellowood.ca
More information about the Python-list
mailing list