在python中,为什么要从内置模块导入“对象"? [英] In python, why import 'object' from builtins module?
问题描述
为了过渡到 python 3,我试图理解编写 python 2 和 python 3 兼容代码.下面的代码来自 python-future.org
,它说明了一种构造一个迭代器的方法,该迭代器兼容两个版本的 python.
from builtins import object类上(对象):def __init__(self, iterable):self._iter = iter(可迭代)def __next__(self): # Py3 风格的迭代器接口return next(self._iter).upper() # 内置 next() 函数调用def __iter__(self):回归自我itr = Upper('你好')assert next(itr) == 'H' # 兼容样式断言列表(itr)==列表('ELLO')
代码在 python 2 中运行良好,但令我惊讶的是,如果我删除导入语句,则会收到错误 TypeError: Upper object is not an iterator
.我经常从 object
派生我的自定义类,但我从未从内置程序中导入它.为什么简单地导入 object
会改变代码的行为?
您正在(间接)从 future.builtins
模块;它提供了一个 custom object
基类,它添加了向前指向的特殊名称.
在 Python 2 中,迭代器必须具有 next()代码>方法
(以及__iter__
);这个方法在 Python 3 中被重命名为 __next__
.如果不使用 future.builtins.object
版本,您只是缺少 Python 2 中提供的 next
-> __next__
别名.
查看源代码对于 future.types.newobject.py
:
def next(self):如果 hasattr(self, '__next__'):返回类型(self).__next__(self)raise TypeError('newobject 不是迭代器')
请注意,如果您运行的是 Python 3,builtins
将返回标准的内置对象,该模块只为 Python 2 返回这些垫片.
您可以简单地自己添加相同的别名:
class Upper(object):def __init__(self, iterable):self._iter = iter(可迭代)def __iter__(self):回归自我def __next__(self): # Py3 风格的迭代器接口return next(self._iter).upper() # 内置 next() 函数调用next = __next__ # Py2 别名
In order to transition to python 3, I am trying to understand writing python 2 and python 3 compatible codes. The following code is from python-future.org
and illustrates a way to construct an iterator that would be compatible with both versions of python.
from builtins import object
class Upper(object):
def __init__(self, iterable):
self._iter = iter(iterable)
def __next__(self): # Py3-style iterator interface
return next(self._iter).upper() # builtin next() function calls
def __iter__(self):
return self
itr = Upper('hello')
assert next(itr) == 'H' # compatible style
assert list(itr) == list('ELLO')
The code runs fine in python 2, but to my surprise, if i remove the import statement then i get an error TypeError: Upper object is not an iterator
. I often derive my custom classes from object
but i have never imported it from builtins. Why does simply importing object
change the behaviour of the code?
You are (indirectly) importing from the future.builtins
module; it provides a custom object
baseclass that adds forward-pointing special names.
In Python 2, iterators must have a next()
method (as well as __iter__
); this method was renamed to __next__
in Python 3. By not using the future.builtins.object
version you are simply missing the next
-> __next__
alias provided in Python 2.
See the source code for future.types.newobject.py
:
def next(self): if hasattr(self, '__next__'): return type(self).__next__(self) raise TypeError('newobject is not an iterator')
Note that builtins
will return standard built-in objects if you are running Python 3, the module only returns shims like these for Python 2.
You could simply add that same alias yourself:
class Upper(object):
def __init__(self, iterable):
self._iter = iter(iterable)
def __iter__(self):
return self
def __next__(self): # Py3-style iterator interface
return next(self._iter).upper() # builtin next() function calls
next = __next__ # Py2 alias
这篇关于在python中,为什么要从内置模块导入“对象"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!