[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

code doesn't reference immutables?

Martin Rinehart

1/7/2008 1:33:00 PM

From the manual:

"code objects are immutable and contain no references (directly or
indirectly) to mutable objects" (3.2)

I thought my code worked with both mutable and immutable objects.
Whassup?

3 Answers

Peter Otten

1/7/2008 1:49:00 PM

0

MartinRinehart wrote:

> From the manual:
>
> "code objects are immutable and contain no references (directly or
> indirectly) to mutable objects" (3.2)
>
> I thought my code worked with both mutable and immutable objects.
> Whassup?

A code object is an internal data structure that describes a piece of
compiled python code. You can create one using compile():

>>> code = compile("a = 42", "<nofile>", "exec")

It is immutable:

>>> code.a = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'code' object has only read-only attributes (assign to .a)

And you can use it like so:

>>> a = "whatever"
>>> exec code
>>> a
42

If you have some spare time you can explore its attributes using
dir(code); otherwise: don't bother.

Peter

Guilherme Polo

1/7/2008 1:54:00 PM

0

2008/1/7, MartinRinehart@gmail.com <MartinRinehart@gmail.com>:
> >From the manual:
>
> "code objects are immutable and contain no references (directly or
> indirectly) to mutable objects" (3.2)
>
> I thought my code worked with both mutable and immutable objects.
> Whassup?
>

What was your intention quoting this half-phrase ?
>From the same place, Python Reference Manual, 3.2:

Code objects:
Code objects represent byte-compiled executable Python code, or
bytecode. The difference between a code object and a function object
is that the function object contains an explicit reference to the
function's globals (the module in which it was defined), while a code
object contains no context; also the default argument values are
stored in the function object, not in the code object (because they
represent values calculated at run-time). Unlike function objects,
code objects are immutable and contain no references (directly or
indirectly) to mutable objects.

> --
> http://mail.python.org/mailman/listinfo/p...
>


--
-- Guilherme H. Polo Goncalves

Terry Reedy

1/8/2008 1:40:00 AM

0


<MartinRinehart@gmail.com> wrote in message
news:5e6d3674-fddb-402f-9e5c-19afcd7fcc22@i7g2000prf.googlegroups.com...
| >From the manual:
|
| "code objects are immutable and contain no references (directly or
| indirectly) to mutable objects" (3.2)
|
| I thought my code worked with both mutable and immutable objects.
| Whassup?

Consider the following:

>>> def g(): return (1,2), [1,2]

>>> dis.dis(g)
1 0 LOAD_CONST 3 ((1, 2))
3 LOAD_CONST 1 (1)
6 LOAD_CONST 2 (2)
9 BUILD_LIST 2
12 BUILD_TUPLE 2
15 RETURN_VALUE

>>> g.func_code.co_consts
(None, 1, 2, (1, 2))

The code object stores the immutables 1, 2, and (1,2) but not the mutable
[1,2]. Rather it stores immutable code to create the mutable list.

I tried to see if the addition of closures violated the stipulation, using

>>> def f():
l = []
def _(x):
l.append(x)
return _

but the inner code object only knows the list by the (immutable) name 'l',
which does not count as a reference. Such code objects cannot be directly
exec'ed but only executed indirectly by calling the function that wraps it
and that has the reference to the in-this-case mutable object. (The
mapping from 'l' to that object appears to be hidden.)

Terry Jan Reedy