473,396 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
5 2872
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
1
by: Andrew James | last post by:
All, I'm having some trouble with understanding python's importing behaviour in my application. I'm using psyco to optimise part of my code, but I'm not sure whether it inherits throughout the...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
3
by: =?Utf-8?B?Tm9yZW1hYw==?= | last post by:
Hi, We are writing a Web SSO service for all of our websites through Forms Authentication. We also want to provide our websites with the ability to protect different parts of their website and...
6
by: Jack | last post by:
I have a set of functions to wrap a library. For example, mylib_init() mylib_func() mylib_exit() or handle = mylib_init() mylib_func(handle)
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.