what should we use instead of the 'new' module?

Robert Kern robert.kern at gmail.com
Wed Nov 12 22:39:32 EST 2008


Flavio wrote:
> How is this code going to look like in Python 3.0? (it's deprecated
> according to http://docs.python.org/library/new.html#module-new, but
> it does not tell what to use instead)
> 
>  method = new.instancemethod(raw_func, None, cls)
>  setattr(cls, name, method)

Use the type objects in the types module.

In [8]: import types

In [9]: class A(object):
    ...:     pass
    ...:

In [10]: def foo(self, x):
    ....:     print x
    ....:
    ....:

In [11]: A.foo = types.MethodType(foo, None, A)

In [12]: a = A()

In [13]: a.foo('See?')
See?

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list