code doesn't reference immutables?

M

MartinRinehart

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?
 
P

Peter Otten

MartinRinehart said:
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():

It is immutable:
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:
42

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

Peter
 
G

Guilherme Polo

2008/1/7 said:
"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.
 
T

Terry Reedy

| >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
(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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top