473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

code doesn't reference immutables?

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?

Jan 7 '08 #1
3 947
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
Jan 7 '08 #2
2008/1/7, Ma************@gmail.com <Ma************@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/python-list

--
-- Guilherme H. Polo Goncalves
Jan 7 '08 #3

<Ma************@gmail.comwrote in message
news:5e**********************************@i7g2000p rf.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

Jan 8 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
20
by: Sam | last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I think it's consistent with every rule but always got compiling errors that I don't understand. The code include 5 files as...
12
by: Bob Brian | last post by:
I was having a discussion with a friend about if the following is valid code. I'm fairly sure it isn't allowed, but my friend seems to think it's fine. It definatly appears to run fine in all the...
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
7
by: George2 | last post by:
Hello everyone, I am reading some code from other people, there are some code like this, class Foo { };
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.