Observations on the three pillars of Python execution
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Aug 5 22:01:51 EDT 2011
Mel wrote:
> Steven D'Aprano wrote:
>
>> There may be some other obscure built-in type that includes code objects,
>> but I can't imagine what it would be. I feel confident in saying that
>> functions, and functions alone, contain code. Even methods are just
>> wrappers around functions. Even built-in functions like len don't contain
>> code! (Or at least, their code isn't accessible from Python.) Which makes
>> sense, if you think about it: their code is part of the Python virtual
>> machine, not the object.
>
> Interesting question. Iterators seem to have code objects:
[...]
Generators. But nice catch, thank you!
Iterators are *any* object which obeys the iterator protocol, that is, have
a next() method and an __iter__() method which behave in the expected way.
Iterators are a duck-type. Generators, whether created from a generator
expression or a generator function, are an actual type.
>>> type(x for x in (1,2))
<class 'generator'>
--
Steven
More information about the Python-list
mailing list