2
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.

4
  • It looks to me like you haven't grasped how this class stuff is meant to work. What you're doing just doesn't make sense. If you haven't grasped it, the Python manual and tutorials are good stuff. Commented Jan 12, 2012 at 10:30
  • It's some kind of interesting... i don't know why test2.newfunc() works... Commented Jan 12, 2012 at 12:06
  • The answer of @Roman is completely correct. When you do test2.newfunc = Test.Func you are 'bypassed' object definition and you go directly into the function definition. So, no parameters are neded. Now it's clear Commented Jan 12, 2012 at 12:11
  • @DonCallisto hello, I have updated the question. As we can see from the updated part, whether we are 'bypassed' object definition or not is not important. What's important is that the retrieval should take place in class instead of instance. Commented Feb 5, 2015 at 8:51

2 Answers 2

6

Methods of a class that are called on an instance of the class are passed the instance reference as an argument automatically. Thus, they're usually declared with a self argument:

class Test:
       def func(self):
           print('func')

test1 = Test()
test1.func() # self = test1
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I'm not good at English. I think i know what you mean. when test1.func() is called on, the instance reference test1 is passed.But when test2.newfunc() is called on, why the instance reference test2 is not passed. You know the same method--func() is called on.
2

A bit of investigation:

>>> test1.func
<bound method Test.func of <__main__.Test instance at 0x800f211b8>>
>>> Test.func
<unbound method Test.func>

Then user-defined bound method (test1.func in our case) is called, this call is actually performed as Test.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:

>>> test1.func
<bound method Test.func of <__main__.Test object at 0xb747624c>>
>>> Test.func
<function func at 0xb74719ec>

As you see, Test.func is a simple function, so no 'magic' will be added while calling it.

1 Comment

Yes. But I still don't understand when test2.newfunc() is called on, why test2 is't passed as an argument.Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.