By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

'class' argument optional for new.instancemethod?

P: n/a
In section "3.27 new -- Creation of runtime internal objects" of the
documentation that comes with Python 2.4 it says:
instancemethod(function, instance, class)

This function will return a method object, bound to instance, or unbound if instance is None. function must be callable.


However, some simple experiments I've tried seem to indicate that the
last argument, 'class' can be left off with no ill effects, and is
therefore optional.

Doesn't anyone know if this is a documention problem? Personally, I
don't understand how the argument gets used when it *is* provided -- if
nothing else, having it seems somewhat redundant given the presence of
the 'instance' argument.

TIA,
Martin

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Martin Miller wrote:
In section "3.27 new -- Creation of runtime internal objects" of the
documentation that comes with Python 2.4 it says:
instancemethod(function, instance, class) [...]
However, some simple experiments I've tried seem to indicate that the
last argument, 'class' can be left off with no ill effects, and is
therefore optional.

Doesn't anyone know if this is a documention problem? Personally, I
don't understand how the argument gets used when it *is* provided -- if
nothing else, having it seems somewhat redundant given the presence of
the 'instance' argument.


A little experimentation reveals at least one thing that
argument affects:
import new
class A: .... pass
.... class B: .... pass
.... def f(self): .... print 'in f', self
.... a = A()
a.f = new.instancemethod(f, a)
a.f() in f <__main__.A instance at 0x00AE5EB8> a.g = new.instancemethod(f, a, B)
a.g() in f <__main__.A instance at 0x00AE5EB8> a.g <bound method B.f of <__main__.A instance at 0x00AE5EB8>> a.f

<bound method ?.f of <__main__.A instance at 0x00AE5EB8>>

So without the class argument, at least in this small
sample, the bound method doesn't know what class
it belongs to, at least for purposes of printing
it's own repr().

-Peter
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.