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

R

Robert Kern

Flavio said:
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
 
F

flrump

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

Flavio said:
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
 
B

Benjamin

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?

Despite what Guido says, the types module will probably still be with
us for years to come, so I wouldn't worry about it.
 
R

Robert Kern

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?

Hmm. Interesting. Anyways, if adding a method to a class is the only use case
you care about, you can just add the function itself. You don't have to make a
method object from it.

In [14]: A.foo = foo

In [16]: a = A()

In [17]: 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
 
J

John Roth

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)

Can we write code in python2.5/2.6 that will work in 3.0?

I'm not sure why your example works: instancemethod is
used internally to wrap the function object when it's
invoked. The only other use for it
is to add a method to an instance, not
to a class. As another poster said, an instance method
in a class is simply the function object. Static and
class methods require wrappers, but those are both
built-in functions and they also have decorators.

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top