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

flrump at gmail.com flrump at gmail.com
Wed Nov 12 22:54:30 EST 2008


Robert,

Appreciate your response.

However Guido says here that types was never intended to be used like
that:

http://bugs.python.org/msg58023

quote: "The types module was only ever intended for type
checking, not for creating new instances.

The correct solution will be to use whatever we end up deciding about
pyvm. Certainly the types module will go."

So that does not seem like a very long term solution to me?

best,

Flavio

On Nov 12, 9:39 pm, Robert Kern <robert.k... at gmail.com> wrote:
> Flavio wrote:
> > How is this code going to look like in Python 3.0? (it's deprecated
> > according tohttp://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