By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

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

P: n/a
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)

Can we write code in python2.5/2.6 that will work in 3.0?
Nov 13 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Nov 13 '08 #2

P: n/a
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...@gmail.comwrote:
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
Nov 13 '08 #3

P: n/a
On Nov 12, 9:54*pm, flr...@gmail.com wrote:
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.

Nov 13 '08 #4

P: n/a
fl****@gmail.com wrote:
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

Nov 13 '08 #5

P: n/a
On Nov 12, 8:32*pm, Flavio <flavio.r...@gmail.comwrote:
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
Nov 13 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.