code doesn't reference immutables?
Peter Otten
__peter__ at web.de
Mon Jan 7 08:48:58 EST 2008
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
More information about the Python-list
mailing list