Perl/Python/Ruby common backend (Parrot, can Ruby play too?)

Christian Tanzer tanzer at swing.co.at
Wed Aug 8 02:25:20 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote:

> I know that Ruby slings around unnamed codeblocks, but I suspect
> that's just a syntax issue (not affecting the backend) wrt the
> Python equivalent, which is slinging around _named_ codeblocks
> (also known as function-objects, method-objects, and other
> callables).  There might be scoping issues, perhaps: in Python,
> any 'codeblock' [actually always a function, even when unnamed,
> as in a lambda-form] has its own scope,

That's only part of the truth. Python also knows about unnamed
codeblocks without scopes. They have type `code` and can be created by
`compile`.

>>> def foo () :
...   return 2*2
... 
>>> foo.func_code 
<code object foo at 0x811e338, file "<stdin>", line 1>
>>> foo.func_code.co_code 
'\x7f\x01\x00\x7f\x02\x00d\x01\x00d\x01\x00\x14Sd\x00\x00S'
>>> bar = compile ("2*2","<string>","eval")
>>> type(bar)
<type 'code'>
>>> bar.co_code
'\x7f\x00\x00d\x00\x00d\x00\x00\x14S'
>>> foo()
4
>>> eval(bar)
4

Nit-picking-ly y'rs, 

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list