Observations on the three pillars of Python execution

Mel mwilson at the-wire.com
Fri Aug 5 14:22:11 EDT 2011


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:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1, 2, 3]
>>> b = (x for x in a)
>>> dir(b)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', 
'__hash__', '__init__', '__iter__', '__name__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', 
'__subclasshook__', 'close', 'gi_code', 'gi_frame', 'gi_running', 'next', 
'send', 'throw']
>>> for name in dir(b):
...   print name, type (getattr (b, name))
... 
__class__ <type 'type'>
__delattr__ <type 'method-wrapper'>
__doc__ <type 'NoneType'>
__format__ <type 'builtin_function_or_method'>
__getattribute__ <type 'method-wrapper'>
__hash__ <type 'method-wrapper'>
__init__ <type 'method-wrapper'>
__iter__ <type 'method-wrapper'>
__name__ <type 'str'>
__new__ <type 'builtin_function_or_method'>
__reduce__ <type 'builtin_function_or_method'>
__reduce_ex__ <type 'builtin_function_or_method'>
__repr__ <type 'method-wrapper'>
__setattr__ <type 'method-wrapper'>
__sizeof__ <type 'builtin_function_or_method'>
__str__ <type 'method-wrapper'>
__subclasshook__ <type 'builtin_function_or_method'>
close <type 'builtin_function_or_method'>
gi_code <type 'code'>
gi_frame <type 'frame'>
gi_running <type 'int'>
next <type 'method-wrapper'>
send <type 'builtin_function_or_method'>
throw <type 'builtin_function_or_method'>


in the form of the gi_code attribute.  No idea what it's for, although no 
reason to believe it shouldn't be there.  (Very interesting demo you gave of 
primitive object creation.  I' awed.)

	Mel.



More information about the Python-list mailing list