MethodType in python 2.2

George Sakkis gsakkis at eden.rutgers.edu
Tue Nov 9 20:31:35 EST 2004


Hi all,

I wonder why types.MethodType fails under python 2.2:

Python 2.2.2 (#1, Jun 25 2003, 18:52:43)
[GCC 3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import types
>>> class X:
...        def f(self,attr): return getattr(self,attr)
...        g = types.MethodType(f,None)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in X
TypeError: cannot create 'instance method' instances


The same code under 2.4 (and I believe 2.3) runs fine.

Python 2.4b1 (#1, Oct 25 2004, 17:07:48)
[GCC 3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import types
>>> class X:
...        def f(self,attr): return getattr(self,attr)
...        g = types.MethodType(f,None)
...
>>> X().f("f")
<bound method X.f of <__main__.X instance at 0x2a97f23e18>>
>>> X().f("g")
<bound method X.f of <__main__.X instance at 0x2a97f23e60>>


Is it possible to get around this in 2.2 ? Thanks in advance,

George




More information about the Python-list mailing list