473,406 Members | 2,371 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,406 software developers and data experts.

python assignment

dan
without stirring the pot too much --

could someone please point me to whatever documentation exists on the
philosophy, semantics, and practical implications of how Python
implements the assignment operator? So far I can't find much useful
in the regular documentation. I understand the basic concept that
names are bound to objects which exist on the heap, but that still
doesn't explain why a = b doesn't _always_ cause a to point to the
same object as b. What determines when the object in question is
copied? What determines when a future maniplulation of the variable
will cause it to point to an object that is already referenced by
another variable? (see code below for an example).

What I need is an exact and unambiguous algorithm for determining when
an assignment will change the id of the variable (or should I say,
when the evaluation of an expression will cause a new object to be
created). Some of the issues involved can be discerned from the
following session:
a = 1
b = a
a is b True a += 1
a -= 1
a is b True a = 1.0
b = a
a is b True a += 1
a -= 1
a is b False a == b

True
Jul 18 '05 #1
8 4507
dan
Ok, thanks for the responses. I think I 'get' it now. However I do
think it would be an excellent idea to have a bit of exposition on
this subject in the Python tutorial, or perhaps in a separate section.
It's not really hard to understand once you realize what's going on,
but I suspect it causes no end of confusion for both new programmers
and old (those used to langs such as C++).

Here's a better example of how a newbie can get confused:
a = (1,2) #a is a tuple
b = a #b & a now point to same object on heap
a += (3,) #tuples are immutable so += returns a new one
b == a #b points to old, a points to new False a = [1,2] #a is a list
b = a #a & b point to same object again
a += [3] #a is mutable, so += mutates it
b == a #of course True a = a + [4] #hmm... one *might* think this is eqiv. to a += [4]
a == b False #but NOOO!! a [1, 2, 3, 4] b
[1, 2, 3]

Now that I understand what's happening, it makes sense, but initially
this sort of behavior was quite mysterious.

-dbm

da*******@yahoo.com (dan) wrote in message news:<fb**************************@posting.google. com>...
without stirring the pot too much --

could someone please point me to whatever documentation exists on the
philosophy, semantics, and practical implications of how Python
implements the assignment operator? So far I can't find much useful
in the regular documentation. I understand the basic concept that
names are bound to objects which exist on the heap, but that still
doesn't explain why a = b doesn't _always_ cause a to point to the
same object as b. What determines when the object in question is
copied? What determines when a future maniplulation of the variable
will cause it to point to an object that is already referenced by
another variable? (see code below for an example).

What I need is an exact and unambiguous algorithm for determining when
an assignment will change the id of the variable (or should I say,
when the evaluation of an expression will cause a new object to be
created). Some of the issues involved can be discerned from the
following session:
a = 1
b = a
a is b True a += 1
a -= 1
a is b True a = 1.0
b = a
a is b True a += 1
a -= 1
a is b False a == b

True

Jul 18 '05 #2
"Tim Peters" <ti*****@comcast.net> writes:
It would be very unusual (because bad design) for the __iadd__
method of a mutable type to return a pre-existing object, though.


I'm confused. I thought that the idea of __iadd__ is that for
*mutable* types it modifies existing object instead of returning new
one. So, was that a typo or are Python lists just bad desing:

l=[]
f=l
f+=[1]
f [1] l

[1]

--
Juha Autero
http://www.iki.fi/jautero/
Eschew obscurity!
Jul 18 '05 #3
In article <sl*******************************@iris.polar.loca l>, Ben Finney wrote:
What I need is an exact and unambiguous algorithm for determining when
an assignment will change the id of the variable


As I understand it, this is specific to the implementation of each type.
The only sensible way to code is as if the identity of an object, on
assignment, were completely unpredictable.

The examples you gave showed that integers share identity with other
integers of the same value, while floats do not.


I believe that's only true for small integers, isn't it?

--
Grant Edwards grante Yow! I want to TAKE IT
at HOME and DRESS IT UP in
visi.com HOT PANTS!!
Jul 18 '05 #4
Grant Edwards wrote:

In article <sl*******************************@iris.polar.loca l>, Ben Finney wrote:
What I need is an exact and unambiguous algorithm for determining when
an assignment will change the id of the variable


As I understand it, this is specific to the implementation of each type.
The only sensible way to code is as if the identity of an object, on
assignment, were completely unpredictable.

The examples you gave showed that integers share identity with other
integers of the same value, while floats do not.


I believe that's only true for small integers, isn't it?


Google finds "LOADI - a fast load instruction for small positive integers
(those referenced by the small_ints array in intobject.c)" and checking
that file is left as an exercise to the reader. I'm going to blindly
assume this is directly related to the issue in question... ;-)

-Peter
Jul 18 '05 #5
> The examples you gave showed that integers share identity with other
> integers of the same value, while floats do not.
I believe that's only true for small integers, isn't it?


Peter> Google finds "LOADI - a fast load instruction for small positive
Peter> integers (those referenced by the small_ints array in
Peter> intobject.c)"

LOADI was an instruction I implemented a few years ago when investigating
bytecode optimization. It was never added to the Python virtual machine
(ceval.c).

Skip

Jul 18 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, 22 Jul 2003 22:46:56 -0400,
Tim Peters <ti*****@comcast.net> wrote:
The rule that the object in question is never copied. There are no
exceptions to this. Copying an object requires invoking some method
of the object, or applying some function to the object. For example,
d.copy() returns a (shallow) copy of a dict d, and L[:] returns a
(shallow) copy of a list L.

To clarify for me please.

A shallow copy of an object, returns an object that contains references
to objects within the copied object, yes?

so a list consisting of [1,2,a,"a"] when copied shallowly, will return
exactly that, but when copied deeply, will reture [1,2,"whatever a
points to","a"] yes?

If I understand the above correctly.... how deep does a deep copy go?
can you vary the depth? Or is it binary, shallow or deep?

Thanks.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/Hr9Bd90bcYOAWPYRAnaYAKDDRFGR6FG3ytgN2dwf7w1shDND3Q Cgs33B
8jNxKhsF1Ti7bkbPzbCxEx4=
=6a1K
-----END PGP SIGNATURE-----

--
Jim Richardson http://www.eskimo.com/~warlock

Linux, because eventually, you grow up enough to be trusted with a fork()
Jul 18 '05 #7
Jim Richardson wrote:
A shallow copy of an object, returns an object that contains
references
to objects within the copied object, yes?

so a list consisting of [1,2,a,"a"] when copied shallowly, will return
exactly that, but when copied deeply, will reture [1,2,"whatever a
points to","a"] yes?


No, that's what the first list already consists of:
a = "whatever a points to"
l1 = [1, 2, a, "a"]
l1 [1, 2, 'whatever a points to', 'a']

A shallow copy makes a new object of the object passed in, and maintains
the same reference. A deep copy makes a new object, _and_ a new object
of all references it contains, and on down the line.

The difference between shallow and deep copying is not apparent when
you're talking about immutable objects, since immutable objects need not
ever be literally copied. Consider a simpler case of a list containing
a single element, which is an instance of a custom class:
class C: pass .... c = C()
x = [c]
y = copy.copy(x)
x is y 0 x[0] is y[0] 1 z = copy.deepcopy(x)
x is z 0 x[0] is z[0] 0 x[0] <__main__.C instance at 0x810ba8c> z[0]

<__main__.C instance at 0x814c9fc>

Shallow copying makes a duplicate of the parent object. Deep copying
makes a duplicate of everything.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Of war men ask the outcome, not the cause.
\__/ Seneca
Jul 18 '05 #8

"Erik Max Francis" <ma*@alcyone.com> wrote in message
news:3F***************@alcyone.com...
immutable objects need not ever be literally copied Shallow copying makes a duplicate of the parent object. Deep copying makes a duplicate of everything.


To be more exact, because first statement is true,
second_statement.replace('everything', 'all mutables'),
which is to say, 'as much as needed'.
import copy
l1=[(),[]]
l2=copy.deepcopy(l1)
for i in l1+l2: print id(i)

....
7669904
8422784
7669904 #same ()
8428448 #different []

Terry J. Reedy

Jul 18 '05 #9

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

Similar topics

0
by: Irmen de Jong | last post by:
QOTW: "Confronting the Martellibot is like flirting with an encyclopedia, I'd rather not do it myself, but I respect those who do, because it produces knowledge." -- Anton...
5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
22
by: Francois | last post by:
I discovered Python a few months ago and soon decided to invest time in learning it well. While surfing the net for Python, I also saw the hype over Ruby and tried to find out more about it, before...
19
by: fyhuang | last post by:
Hello all, I've been wondering a lot about why Python handles classes and OOP the way it does. From what I understand, there is no concept of class encapsulation in Python, i.e. no such thing as...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
13
by: John Dann | last post by:
A Python newbie, but some basic understanding of how classes, objects etc work in eg VB.Net. However, I'm struggling a little to translate this knowledge into the Python context. I'm trying to...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.