from new import classobj in Python 3.1
How can I change the given python 2.x statement to get it compiled on 3.1.
The code lin开发者_如何学运维e that not works.
from new import classobj
The error is "There is no module new".
In Python 2.x new.classobj
is the type of old-style types. There are no old-style types in Python 3.x. To port your code from Python 2.x to Python 3.x you first need to bring it up to the latest Python 2.x standards, and in that case it means stop using old-style classes and only use new-style classes.
So the answer is instead of classobj
use type
, but you'll need to upgrade the 2.x code to use type
first and then look at porting to Python 3.x
BTW, See PEP 3108 for the list of modules removed in Python 3.x. In particular:
new
- Just a rebinding of names from the 'types' module.
- Can also call type built-in to get most types easily.
- Docstring states the module is no longer useful as of revision 27241
(2002-06-15).
You could use the type
function:
X = type('X', (object,), {'a': 1})
If you are in a package, you need to change it to a relative import:
from .new import classobj
精彩评论