class Test:
def func():
print('func')
test1 = Test()
test2 = Test()
test1.func() #TypeError: fun1() takes no arguments (1 given)
test2.newfunc = Test.func
test2.newfunc()#It goes well
# update part
def foo(self):
pass
Test.foo = foo
test1.foo # it is bound method
test2.foo = foo
test2.foo # it is function
# end
Is there any difference between the two ways ?
Thanks.
# update part
def foo(self):
pass
Test.foo = foo
test1.foo # it is bound method
test2.foo = foo
test2.foo # it is function
# end
Note that what’s important is that the retrieval should take place in class instead of instance.
A bit of investigation:
Then user-defined bound method (
test1.funcin our case) is called, this call is actually performed asTest.func(test1)– class instance is always passed as first argument.See much more on this topic in Python Data model.
Edit
Output above is from Python 2.6. Since
Test.func()works for you, I assume that you are using Python 3. In this case output will be the next:As you see,
Test.funcis a simple function, so no ‘magic’ will be added while calling it.