473,385 Members | 1,893 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.

different binding behavior

It seems to me that the following behavior of python (2.4.1) is inconsistent:
a=1
b=a
a+=1
b 1 a 2 a=[1,2]
b=a
b+=[3]
a [1, 2, 3] b

[1, 2, 3]

Why was it implemented like this??

Best regards,
Gabriel.

--
/-----------------------------------------------------------------------\
| Any intelligent fool can make things bigger, more complex, |
| or more violent. It takes a touch of genius - and a lot of courage - |
| to move in the opposite direction. (Einstein) |
\-----------------------------------------------------------------------/
Nov 10 '05 #1
8 1049
Gabriel Zachmann wrote:
It seems to me that the following behavior of python (2.4.1) is inconsistent: [snip] Why was it implemented like this??


Lists are mutable objects; integers are not. For a list, a += b is
equivalent to a.__iadd__(b), which is an in-place modification.

For an integer, no __iadd__ method is provided, so a += b is equivalent
to a = a.__add__(b), which is a rebinding operation rather than a
modification.

This question (or variants thereof) is asked on an almost daily basis;
please search before posting.

-- David

Nov 10 '05 #2
Gabriel Zachmann wrote:
It seems to me that the following behavior of python (2.4.1) is
inconsistent:
>>> a=1
>>> b=a
>>> a+=1
>>> b 1 >>> a 2 >>> a=[1,2]
>>> b=a
>>> b+=[3]
>>> a [1, 2, 3] >>> b

[1, 2, 3]

Why was it implemented like this??


assuming that you know that integers cannot modified in place, and
that += on a list is defined to be the same thing as a call to extend,
what did you expect this to do?

</F>

Nov 10 '05 #3
On Thu, 10 Nov 2005 22:43:53 +0100, Gabriel Zachmann wrote:
It seems to me that the following behavior of python (2.4.1) is inconsistent:

We've just had a HUGE thread arguing about this behaviour, just three or
five days ago. Let's not start it again.

In a nutshell, the behaviour is because ints are immutable and can't be
changed in place, and lists are mutable and can be changed in place.

Imagine that ints could be changed in place. Then you could do this:

x = 0
x += 1

Now the int 0 has the value of 1. So:

print 2 + 0
=> prints 3

Confusion and horror.

So of course x += 1 can't change the int in place, it has to rebind x to a
new int.

--
Steven.

Nov 10 '05 #4
Steven D'Aprano <st***@REMOVETHIScyber.com.au> writes:
Imagine that ints could be changed in place. Then you could do this:

x = 0
x += 1


No nothing like that. Nothing stops you from having multiple int
objects with the same value. Lists, for example, are mutable, but

x = [0,1]
x += [2,3]

doesn't change what the literal [0,1] means.
Nov 10 '05 #5
> We've just had a HUGE thread arguing about this behaviour, just three or
five days ago. Let's not start it again.
ok, could you please point me to it?
In a nutshell, the behaviour is because ints are immutable and can't be
changed in place, and lists are mutable and can be changed in place.

Imagine that ints could be changed in place. Then you could do this:


i don't see why there should be only one instance of Int with the value 0.
But if all this has already been debated (and, apparently, my point didn't
succeed), there is no need to discuss all this over again.

In any case, thanks a lot for your response and summary.

Best regards,
Gabriel.

--
/-----------------------------------------------------------------------\
| Any intelligent fool can make things bigger, more complex, |
| or more violent. It takes a touch of genius - and a lot of courage - |
| to move in the opposite direction. (Einstein) |
\-----------------------------------------------------------------------/
Nov 22 '05 #6
> We've just had a HUGE thread arguing about this behaviour, just three or
five days ago. Let's not start it again.
ok, could you please point me to it?
In a nutshell, the behaviour is because ints are immutable and can't be
changed in place, and lists are mutable and can be changed in place.

Imagine that ints could be changed in place. Then you could do this:


i don't see why there should be only one instance of Int with the value 0.
But if all this has already been debated (and, apparently, my point didn't
succeed), there is no need to discuss all this over again.

In any case, thanks a lot for your response and summary.

Best regards,
Gabriel.

--
/-----------------------------------------------------------------------\
| Any intelligent fool can make things bigger, more complex, |
| or more violent. It takes a touch of genius - and a lot of courage - |
| to move in the opposite direction. (Einstein) |
\-----------------------------------------------------------------------/
Nov 22 '05 #7
On Fri, 18 Nov 2005 15:34:36 +0100, Gabriel Zachmann wrote:
i don't see why there should be only one instance of Int with the value 0.


"Small" ints are cached, so there may be only one instance of the int with
value 0. However, that's an implementation detail, which may change from
version to version, and is not generally true:

py> x = 0; y = 3-3
py> x == y
True
py> x is y
True

py> x = 357900001; y = 3579*100000+1
py> x == y
True
py> x is y
False

So in general, there can be many instances of int with the same value.
That's not what immutable means.

--
Steven.

Nov 22 '05 #8
On Fri, 18 Nov 2005 15:34:36 +0100, Gabriel Zachmann wrote:
i don't see why there should be only one instance of Int with the value 0.


"Small" ints are cached, so there may be only one instance of the int with
value 0. However, that's an implementation detail, which may change from
version to version, and is not generally true:

py> x = 0; y = 3-3
py> x == y
True
py> x is y
True

py> x = 357900001; y = 3579*100000+1
py> x == y
True
py> x is y
False

So in general, there can be many instances of int with the same value.
That's not what immutable means.

--
Steven.

Nov 22 '05 #9

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

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
0
by: RPI_alum | last post by:
I've been experiencing some frustrating behavior that I believe may be a MS bug in the Framework. Has anyone else experienced this, know a better way to resolve it, or if it is an actual MS bug ...
9
by: Marina | last post by:
Here is the problem. If 2 different properties on the same (or different) control are bound to the same data column, changing the Text property and calling EndCurrentEdit discards the new value. ...
1
by: Jonathan Yong | last post by:
I observe a very weird behavior when dynamically create web control and bind events to it. Create a C# ASP.NET application, Put a PlaceHolder and Textbox onto the Web form, and try with the 4...
1
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
9
by: VidTheKid | last post by:
Hello everybody. I just became the webmaster of an organization who wants a marquee on the front page. I don't have problems using a marquee sensibly. I actually have some structured content...
6
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document...
6
by: Mikus Sleiners | last post by:
Is there any way to enable exception throws in VS 2005, that occur during binding operations? I am upset that i can't see exceptions that are thrown during binding operations. It's very hard to...
6
by: Dmitry Duginov | last post by:
Hi, I have the following label markup (label is inside FormView): <asp:Label ID="lblIndicatorReady" runat="server" Text="RE" ToolTip="Ready" BackColor='<%#...
6
by: Tomasz J | last post by:
Hello developers, I bind my TextBox control specyfying a format stored in my application global ApplicationContext object - it has a static string CurrencyFormat property. The problem - this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...

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.