473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

UnboundMethodType and MethodType

Hello all,
class Q: .... def bar(self):
.... pass
.... import types
types.UnboundMethodType is types.MethodType True
type(Q.bar) <type 'instancemethod'>
q = Q()
type(q.bar) <type 'instancemethod'>
type(q.bar) is types.UnboundMethodType True q.bar <bound method Q.bar of <__main__.Q instance at 0x4042756c>>


I think is not very consistent
notice q.bar is bounded although type(q.bar)
says it's types.UnboundedMethodType
what do you think?

Regard, Daniel

Feb 8 '06 #1
6 1993
Schüle Daniel wrote:
Hello all,
>>> class Q: ... def bar(self):
... pass
... >>> import types
>>> types.UnboundMethodType is types.MethodType True >>>
>>> type(Q.bar) <type 'instancemethod'> >>>
>>> q = Q()
>>> type(q.bar) <type 'instancemethod'> >>>
>>> type(q.bar) is types.UnboundMethodType True >>> q.bar <bound method Q.bar of <__main__.Q instance at 0x4042756c>> >>>
I think is not very consistent
notice q.bar is bounded although type(q.bar)
says it's types.UnboundedMethodType
what do you think?

Regard, Daniel


I think it's perfectly consistent:
class B(object): .... def bar(self): pass
.... B.bar <unbound method B.bar> type(B.bar) <type 'instancemethod'> b = B()
b.bar <bound method B.bar of <__main__.B object at 0xb7bd544c>> type(b.bar) <type 'instancemethod'> id(B.bar) -1211888788 id(b.bar) -1211888788

It's the same function, whether it's bound or not. Thus, it should
always have the same type. It's simply called in different ways. You can
just as easily say:
B.bar(b)
As:
b.bar()


-Kirk McDonald
Feb 8 '06 #2
Kirk McDonald wrote:
I think it's perfectly consistent:
>>> class B(object): ... def bar(self): pass
... >>> B.bar <unbound method B.bar> >>> type(B.bar) <type 'instancemethod'> >>> b = B()
>>> b.bar <bound method B.bar of <__main__.B object at 0xb7bd544c>> >>> type(b.bar) <type 'instancemethod'> >>> id(B.bar) -1211888788 >>> id(b.bar) -1211888788

It's the same function, whether it's bound or not. Thus, it should
always have the same type.


No, it's not the same function. You got the same id because you didn't
bind B.bar and b.bar to anything so the id was reused.
class B(object): ... def bar(self): pass
... Bbar = B.bar
bbar = B().bar
Bbar <unbound method B.bar> bbar <bound method B.bar of <__main__.B object at 0x008759B0>> id(Bbar) 10751312 id(bbar) 10736624 Bbar is bbar

False

Kent
Feb 8 '06 #3
Kent Johnson wrote:
Kirk McDonald wrote:

....
>>> id(B.bar)

-1211888788
>>> id(b.bar)

-1211888788

It's the same function, whether it's bound or not....


No, it's not the same function. You got the same id because you didn't
bind B.bar and b.bar to anything so the id was reused.
>>> class B(object): ... def bar(self): pass
... >>> Bbar = B.bar
>>> bbar = B().bar
>>> Bbar <unbound method B.bar> >>> bbar <bound method B.bar of <__main__.B object at 0x008759B0>> >>> id(Bbar) 10751312 >>> id(bbar) 10736624 >>> Bbar is bbar False

Kent


To elaborate on this, once 'id' is called, you drop the reference.
This allows quite surprising things like:
id(7**8) == id(8**7) True a, b = 7**8, 8**7
id(a) == id(b) # this time there are other references to a and b False

If you wanted to test the original code for identity match: B.bar is B().bar

False
is the appropriate test (the 'is' test holds the identities through
the comparison).

By the by, this is tricky stuff, nobody should expect to understand
it thoroughly without both study and testing.

--Scott David Daniels
sc***********@acm.org
Feb 8 '06 #4
[...]
It's the same function, whether it's bound or not. Thus, it should
always have the same type.

No, it's not the same function. You got the same id because you didn't
bind B.bar and b.bar to anything so the id was reused.


thank you for the explanation
it's indeed tricky with reusing the id
are there some pages on the net to read more about it?
class Q: .... def __init__(self,val):
.... self.val = val
.... def bar(self):
.... print self.val
.... x = Q.bar
y = Q("test").bar
x <unbound method Q.bar> y <bound method Q.bar of <__main__.Q instance at 0x40427a6c>> y() test id(x) 1078081812 id(y) 1078082492


the same id-value would enforce two objects to be
of the same type (trivial case)
the reverse is not necessarily true
in this case x.bar and y.bar are of the same type

UnboundMethodType is still not very suitable name for Q().bar
:-/

Regards, Daniel

Feb 8 '06 #5
Scott David Daniels wrote:
To elaborate on this, once 'id' is called, you drop the reference.
This allows quite surprising things like:
>>> id(7**8) == id(8**7) True >>> a, b = 7**8, 8**7
>>> id(a) == id(b) # this time there are other references to a and b False

If you wanted to test the original code for identity match: >>> B.bar is B().bar

False
is the appropriate test (the 'is' test holds the identities through
the comparison).

By the by, this is tricky stuff, nobody should expect to understand
it thoroughly without both study and testing.

--Scott David Daniels
sc***********@acm.org


You know what? That makes perfect sense. Thank you.

-Kirk McDonald
Feb 8 '06 #6
Kirk McDonald wrote:
Scott David Daniels wrote: <an elaboration on ids and refcounts>

You know what? That makes perfect sense. Thank you.


Thanks a lot for mentioning this. I do try to help out, and sometimes
it feels like talking to the wind. A thanks every now and then is
greatly appreciated.

Just for fun, you can play with:

import sys
sys.getrefcount(123**18)
vs.
sys.getrefcount(12)

You should know that 'small' integers are kept and shared once built.
Similarly, some strings (including all one-character strings and strings
that might be identifiers).
sys.getrefcount('a')
sys.getrefcount('probably_not_really_a_variable')

--Scott David Daniels
sc***********@acm.org
Feb 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: George Sakkis | last post by:
Hi all, I wonder why types.MethodType fails under python 2.2: Python 2.2.2 (#1, Jun 25 2003, 18:52:43) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>...
11
by: Alex Popescu | last post by:
Hi all! I am pretty new to Python, so please excuse me if I am missing something. Lately, I've been playing with decorators and I am a bit confused about some behavior. Here is the code that...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.