__builtin__.iterator不存在?
考虑一下:
Python 2.7.3 (default, Aug 2 2012, 16:55:39)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import __builtin__
>>> type(iter('123'))
<type 'iterator'>
>>> type(iter('123')).__class__
<type 'type'>
>>> type(iter('123')).__name__
'iterator'
>>> type(iter('123')).__module__
'__builtin__'
>>> __builtin__.iterator
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'iterator'
>>> dir(__builtin__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
>>>
我们是不是可以认为 __builtin__.iterator
这个东西其实并不存在?有没有合适的方法可以获取到这个类的引用,而不需要通过 iter
创建一个临时的实例呢?
2 个回答
更糟糕的是,这种类型并不是一个直接的类。
>>> type(iter(range(5)))
<type 'listiterator'>
当你询问一个(新式)类的实例它的类型时,它会告诉你它是一个类。
>>> class X(object): pass
...
>>> type(X())
<class '__main__.X'>
(那他们为什么要用旧式类来实现新功能呢?)
既然如此,如果你碰巧得到了它,你会怎么处理呢?
好吧,你可以找到它,如果你仔细研究一下,里面确实有一个类。
>>> it = iter(range(5)).__class__
但是你不能从它那里继承。
>>> class it2(it): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type 'listiterator' is not an acceptable base type
而且对它进行“猴子补丁”似乎也很不道德。我希望有什么东西能阻止你。
你也不能直接实例化它:
>>> x = it()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot create 'listiterator' instances
所以它是不是更好假装不存在,而不是出现后为自己不能做的正常事情道歉呢?
同样的情况也适用于function、int、double、str等类型。大多数至少让你实例化它们,但它们并不是真正的类,所以深入了解它们会让人感到害怕。
并不是所有的内部对象类型都在 __builtin__
这个结构里列出来。
你在这里也找不到方法类型、生成器类型,或者其他很多类型。只有那些你在 Python 代码中实际会用到的名称才会出现在这个命名空间里。
一些这样的类型在 types
模块中列出,但迭代器类型没有,因为正如 源代码所说:
# Iterators in Python aren't a matter of type but of protocol. A large
# and changing number of builtin types implement *some* flavor of
# iterator. Don't check the type! Use hasattr to check for both
# "__iter__" and "next" attributes instead.
如果你想测试某个东西是否是迭代器,应该使用 Iterator
ABC。
import collections
if isinstance(something, collections.Iterator):
如果你有一些特殊的需求想要获取字符串返回的 iterator
类型,可以对它使用 type()
:
>>> type(iter('123'))
<type 'iterator'>
然后把结果存起来;这就是 types
模块生成许多引用的方式。
不过,要知道这个类型并不是通用的:
>>> iterator = type(iter(''))
>>> isinstance(iter([]), iterator)
False
>>> iter([])
<listiterator object at 0x108e72d50>
>>> isinstance(reversed([]), iterator)
False
>>> reversed([])
<listreverseiterator object at 0x108e72d50>
但是使用 ABC 测试的话可以识别所有的迭代器:
>>> from collections import Iterator
>>> isinstance(iter([]), Iterator)
True
>>> isinstance(iter(reversed([])), Iterator)
True
>>> isinstance(iter(''), Iterator)
True
如果你需要创建一个迭代器,可以生成一个序列并返回对 iter()
的结果,或者自己创建一个迭代器类型。后者也很简单,可以使用生成器表达式,或者为 __iter__
使用生成器函数,或者给你的类型添加一个 __next__
方法,每次调用时返回一个项目(并让 __iter__
返回 self
)。