Re: tuples in conditional assignment (Ben Finney)

2015-11-24 Thread George Trojan
Ben Finney writes: Ben Finney Date: 11/24/2015 04:49 AM To: python-list@python.org George Trojan writes: The following code has bitten me recently: t=(0,1) x,y=t if t else 8, 9 print(x, y) (0, 1) 9 You can simplify this by taking assignment out of the picture:: >>> t = (0, 1)

Re: tuples in conditional assignment

2015-11-24 Thread Steven D'Aprano
On Tue, 24 Nov 2015 02:25 pm, George Trojan wrote: > The following code has bitten me recently: > > >>> t=(0,1) > >>> x,y=t if t else 8, 9 > >>> print(x, y) > (0, 1) 9 > > I was assuming that a comma has the highest order of evaluation, that is > the expression 8, 9 should make a tuple. Why t

Re: tuples in conditional assignment

2015-11-23 Thread Ben Finney
George Trojan writes: > The following code has bitten me recently: > > >>> t=(0,1) > >>> x,y=t if t else 8, 9 > >>> print(x, y) > (0, 1) 9 You can simplify this by taking assignment out of the picture:: >>> t = (0, 1) >>> t if t else 8, 9 ((0, 1), 9) So that's an “expression list”

Re: Tuples and immutability

2014-03-12 Thread Terry Reedy
On 3/12/2014 9:35 PM, Ian Kelly wrote: On Wed, Mar 12, 2014 at 5:20 PM, Steven D'Aprano wrote: On Tue, 11 Mar 2014 17:06:43 -0600, Ian Kelly wrote: That's true but irrelevant to my point, which was to counter the assertion that mutable types can always be assumed to be able to perform operati

Re: Tuples and immutability

2014-03-12 Thread Ian Kelly
On Wed, Mar 12, 2014 at 5:20 PM, Steven D'Aprano wrote: > On Tue, 11 Mar 2014 17:06:43 -0600, Ian Kelly wrote: > >> That's true but irrelevant to my point, which was to counter the >> assertion that mutable types can always be assumed to be able to perform >> operations in-place. > > "Always"? Not

Re: Tuples and immutability

2014-03-12 Thread Steven D'Aprano
On Tue, 11 Mar 2014 17:06:43 -0600, Ian Kelly wrote: > On Tue, Mar 11, 2014 at 10:46 AM, Steven D'Aprano > wrote: >>> There are a number of possible solutions. One possibility would be to >>> copy the Circle as an Ellipse and return the new object instead of >>> mutating it. Then you have the si

Re: Tuples and immutability

2014-03-12 Thread Antoon Pardon
Op 12-03-14 10:51, Ian Kelly schreef: > On Wed, Mar 12, 2014 at 3:39 AM, Antoon Pardon > wrote: >> The documentation is wrong at that point as the following code illustrates. > Either way it still has to do a getitem and a setitem, but if you have > a more nested structure then the extra getitems

Re: Tuples and immutability

2014-03-12 Thread Oscar Benjamin
On 12 March 2014 03:25, Terry Reedy wrote: > On 3/11/2014 10:01 PM, Rick Johnson wrote: >> >> >> On Thursday, February 27, 2014 4:18:01 PM UTC-6, Ian wrote: >>> >>> x += y is meant to be equivalent, except possibly in-place and >>> more efficient, than x = x + y. > > > The manual actually says "An

Re: Tuples and immutability

2014-03-12 Thread Ian Kelly
On Wed, Mar 12, 2014 at 3:39 AM, Antoon Pardon wrote: > The documentation is wrong at that point as the following code illustrates. Either way it still has to do a getitem and a setitem, but if you have a more nested structure then the extra getitems are not repeated. For example, using your log

Re: Tuples and immutability

2014-03-12 Thread Ian Kelly
On Wed, Mar 12, 2014 at 12:28 AM, Steven D'Aprano wrote: > On Tue, 11 Mar 2014 23:25:19 -0400, Terry Reedy wrote: >> Nope, 'similar' is not 'equivalent'. Evaluating x twice instead of once >> and possibly allocating a new object versus not take extra time. In a >> statement like "x.y.z[3*n+m] += 1

Re: Tuples and immutability

2014-03-12 Thread Antoon Pardon
Op 12-03-14 07:28, Steven D'Aprano schreef: > On Tue, 11 Mar 2014 23:25:19 -0400, Terry Reedy wrote: > >> Nope, 'similar' is not 'equivalent'. Evaluating x twice instead of once >> and possibly allocating a new object versus not take extra time. In a >> statement like "x.y.z[3*n+m] += 1", calculati

Re: Tuples and immutability

2014-03-12 Thread Ethan Furman
On 03/11/2014 08:25 PM, Terry Reedy wrote: On 3/11/2014 10:01 PM, Rick Johnson wrote: On Thursday, February 27, 2014 4:18:01 PM UTC-6, Ian wrote: x += y is meant to be equivalent, except possibly in-place and more efficient, than x = x + y. The manual actually says "An augmented assignment e

Re: Tuples and immutability

2014-03-11 Thread Steven D'Aprano
On Tue, 11 Mar 2014 23:25:19 -0400, Terry Reedy wrote: > On 3/11/2014 10:01 PM, Rick Johnson wrote: >> >> On Thursday, February 27, 2014 4:18:01 PM UTC-6, Ian wrote: >>> x += y is meant to be equivalent, except possibly in-place and more >>> efficient, than x = x + y. > > The manual actually says

Re: Tuples and immutability

2014-03-11 Thread Terry Reedy
On 3/11/2014 10:01 PM, Rick Johnson wrote: On Thursday, February 27, 2014 4:18:01 PM UTC-6, Ian wrote: x += y is meant to be equivalent, except possibly in-place and more efficient, than x = x + y. The manual actually says "An augmented assignment expression like x += 1 can be rewritten as x

Re: Tuples and immutability

2014-03-11 Thread Rick Johnson
On Thursday, February 27, 2014 4:18:01 PM UTC-6, Ian wrote: > x += y is meant to be equivalent, except possibly in-place and more > efficient, than x = x + y. In an ideal world, the speed of these two codes should be the same, of course i'm "assuming" that most competent language designers wou

Re: Tuples and immutability

2014-03-11 Thread Chris Angelico
On Wed, Mar 12, 2014 at 1:01 PM, Rick Johnson wrote: > On Thursday, February 27, 2014 4:18:01 PM UTC-6, Ian wrote: >> x += y is meant to be equivalent, except possibly in-place and more >> efficient, than x = x + y. > > In an ideal world, the speed of these two codes should be the same, of course

Re: Tuples and immutability

2014-03-11 Thread Ian Kelly
On Tue, Mar 11, 2014 at 10:46 AM, Steven D'Aprano wrote: >> There are a number of possible solutions. One possibility would be to >> copy the Circle as an Ellipse and return the new object instead of >> mutating it. Then you have the situation where, given a mutable object >> x that satisfies isi

Re: Tuples and immutability

2014-03-11 Thread Gregory Ewing
Steven D'Aprano wrote: On Tue, 11 Mar 2014 04:39:39 -0600, Ian Kelly wrote: On Mon, Mar 10, 2014 at 11:03 PM, Gregory Ewing > What's the obvious way to spell in-place set intersection, for example? I would expect it to be &=, That's my point -- once you know the binary operator for an op

Re: Tuples and immutability

2014-03-11 Thread Steven D'Aprano
On Tue, 11 Mar 2014 04:39:39 -0600, Ian Kelly wrote: > On Mon, Mar 10, 2014 at 11:03 PM, Gregory Ewing > wrote: >> As far as observable effects are concerned, it's quite clear: mutable >> objects can *always* be updated in-place, and immutable objects can >> *never* be. > > Hm. Consider the circ

Re: Tuples and immutability

2014-03-11 Thread Ian Kelly
On Mon, Mar 10, 2014 at 11:03 PM, Gregory Ewing wrote: > As far as observable effects are concerned, it's > quite clear: mutable objects can *always* be updated > in-place, and immutable objects can *never* be. Hm. Consider the circle-ellipse problem. Briefly, a circle is-an ellipse, so in an in

Re: Tuples and immutability

2014-03-10 Thread Gregory Ewing
Ian Kelly wrote: If the in-place behavior of += is held to be part of the interface, then we must accept that += is not polymorphic across mutable and immutable types, That's quite correct, it's not. As I said, it's one notation doing double duty. Usually there isn't any confusion, because you

Re: Tuples and immutability

2014-03-10 Thread Gregory Ewing
Ian Kelly wrote: It's technically "possible" for this augmented assignment to be performed in place: x = 12 x += 4 But it's not done in-place, because ints are meant to be immutable. Which means it's *not* possible, because doing so would violate the documented properties of the int type. I

Re: Tuples and immutability

2014-03-10 Thread Steven D'Aprano
On Mon, 10 Mar 2014 02:35:36 -0600, Ian Kelly wrote: > On Sun, Mar 9, 2014 at 8:37 PM, Steven D'Aprano > wrote: >> On Sun, 09 Mar 2014 17:42:42 -0600, Ian Kelly wrote: >> >>> On Sun, Mar 9, 2014 at 4:03 PM, Gregory Ewing >>> wrote: >> Note that it says "when possible", not "if the implement

Re: Tuples and immutability

2014-03-10 Thread Ian Kelly
On Sun, Mar 9, 2014 at 8:37 PM, Steven D'Aprano wrote: > On Sun, 09 Mar 2014 17:42:42 -0600, Ian Kelly wrote: > >> On Sun, Mar 9, 2014 at 4:03 PM, Gregory Ewing >> wrote: > >>> Note that it says "when possible", not "if the implementation feels >>> like it". >> >> That's quite vague, and not much

Re: Tuples and immutability

2014-03-09 Thread Steven D'Aprano
On Sun, 09 Mar 2014 17:42:42 -0600, Ian Kelly wrote: > On Sun, Mar 9, 2014 at 4:03 PM, Gregory Ewing > wrote: >> Note that it says "when possible", not "if the implementation feels >> like it". > > That's quite vague, and not much stronger a guarantee than "maybe". It's > technically "possible"

Re: Tuples and immutability

2014-03-09 Thread Ian Kelly
On Sun, Mar 9, 2014 at 4:03 PM, Gregory Ewing wrote: > Ian Kelly wrote: >> >> In my view the second one is wrong. a += b should be understood as >> being equivalent to a = a + b, but with the *possible* and by no means >> guaranteed optimization that the operation may be performed in-place. > > >

Re: Tuples and immutability

2014-03-09 Thread Terry Reedy
On 3/9/2014 6:03 PM, Gregory Ewing wrote: Ian Kelly wrote: In my view the second one is wrong. a += b should be understood as being equivalent to a = a + b, but with the *possible* and by no means guaranteed optimization that the operation may be performed in-place. This interpretation is at

Re: Tuples and immutability

2014-03-09 Thread Gregory Ewing
Ian Kelly wrote: In my view the second one is wrong. a += b should be understood as being equivalent to a = a + b, but with the *possible* and by no means guaranteed optimization that the operation may be performed in-place. This interpretation is at odds with the Language Reference, section 6

Re: Tuples and immutability

2014-03-09 Thread Chris Angelico
On Mon, Mar 10, 2014 at 6:57 AM, Joshua Landau wrote: > I would probably implement it closer to home. Inside > tuple.__getitem__, there would be something like > > if context_is_augmented_assignment(): > raise TypeError(message+warning) > else: > raise TypeError(message) >

Re: Tuples and immutability

2014-03-09 Thread Joshua Landau
On 9 March 2014 18:13, Chris Angelico wrote: > I think I see what you're saying here. But ignore "top-level"; this > should just be a part of the exception message, no matter what. I don't think I was clear, but yes. That. > What you're saying is that this should notice that it's doing an > augm

Re: Tuples and immutability

2014-03-09 Thread Chris Angelico
On Mon, Mar 10, 2014 at 4:54 AM, Joshua Landau wrote: > On 28 February 2014 14:43, Chris Angelico wrote: >> On Sat, Mar 1, 2014 at 1:41 AM, Joshua Landau wrote: >>> Would it be better to add a check here, such that if this gets raised >>> to the top-level it includes a warning ("Addition was inp

Re: Tuples and immutability

2014-03-09 Thread Joshua Landau
On 28 February 2014 14:43, Chris Angelico wrote: > On Sat, Mar 1, 2014 at 1:41 AM, Joshua Landau wrote: >> Would it be better to add a check here, such that if this gets raised >> to the top-level it includes a warning ("Addition was inplace; >> variable probably mutated despite assignment failur

Re: Tuples and immutability

2014-03-09 Thread Marko Rauhamaa
Ian Kelly : > In my view the second one is wrong. a += b should be understood as > being equivalent to a = a + b, but with the *possible* and by no means > guaranteed optimization that the operation may be performed in-place. Some call it an optimization, others call it a side effect. Anyway, th

Re: Tuples and immutability

2014-03-08 Thread Ian Kelly
On Sat, Mar 8, 2014 at 5:45 PM, Gregory Ewing wrote: > Ian Kelly wrote: > >> I already mentioned this earlier in the thread, but a balanced binary >> tree might implement += as node insertion and then return a different >> object if the balancing causes the root node to change. > > > That would be

Re: Tuples and immutability

2014-03-08 Thread Ian Kelly
On Sat, Mar 8, 2014 at 5:40 PM, Gregory Ewing wrote: > Ian Kelly wrote: >> >> class LessThanFilter: >> >> def __init__(self, the_list): >> self._the_list = the_list >> >> def __getitem__(self, bound): >> return [x for x in self._the_list if x < bound] >> >> >> filter = Less

Re: Tuples and immutability

2014-03-08 Thread Gregory Ewing
Ian Kelly wrote: I already mentioned this earlier in the thread, but a balanced binary tree might implement += as node insertion and then return a different object if the balancing causes the root node to change. That would be a really bad way to design a binary tree implementation. What if th

Re: Tuples and immutability

2014-03-08 Thread Gregory Ewing
Ian Kelly wrote: class LessThanFilter: def __init__(self, the_list): self._the_list = the_list def __getitem__(self, bound): return [x for x in self._the_list if x < bound] filter = LessThanFilter([10, 20, 30, 40, 50]) filter[25] += [15, 17, 23] Should that last line

Re: Balanced trees (was: Re: Tuples and immutability)

2014-03-08 Thread Dan Stromberg
On Sat, Mar 8, 2014 at 12:34 AM, Marko Rauhamaa wrote: > Ian Kelly : > >> I already mentioned this earlier in the thread, but a balanced binary >> tree might implement += as node insertion and then return a different >> object if the balancing causes the root node to change. > > True. > > Speaking

Re: Balanced trees (was: Re: Tuples and immutability)

2014-03-08 Thread Ian Kelly
On Sat, Mar 8, 2014 at 1:34 AM, Marko Rauhamaa wrote: > Speaking of which, are there plans to add a balanced tree to the > "batteries" of Python? Timers, cache aging and the like need it. I'm > using my own AVL tree implementation, but I'm wondering why Python > still doesn't have one. None curre

Balanced trees (was: Re: Tuples and immutability)

2014-03-08 Thread Marko Rauhamaa
Ian Kelly : > I already mentioned this earlier in the thread, but a balanced binary > tree might implement += as node insertion and then return a different > object if the balancing causes the root node to change. True. Speaking of which, are there plans to add a balanced tree to the "batteries"

Re: Tuples and immutability

2014-03-07 Thread Ian Kelly
On Fri, Mar 7, 2014 at 7:17 PM, Gregory Ewing wrote: > Here's another idea: If the __iadd__ method returns the > same object, *and* the LHS doesn't have a __setitem__ > method, then do nothing instead of raising an exception. Maybe it doesn't have a __setitem__ because the object that was retriev

Re: Tuples and immutability

2014-03-07 Thread Gregory Ewing
Duncan Booth wrote: Is there any reason why tuples need to throw an exception on assigning to the element if the old value and new value are the same object? It would make introspection misleading, because tuples would have a __setitem__ method event though they don't actually support item assi

Re: Tuples and immutability

2014-03-07 Thread Ian Kelly
On Fri, Mar 7, 2014 at 4:51 AM, Alister wrote: > I would think it would be better if the exception was thrown before the > assignment to the list took place > simply seeing that a modification action was being applied to a tupple > should be enough. > this would alert the programmer to the fact th

Re: Tuples and immutability

2014-03-07 Thread Roy Smith
In article , Duncan Booth wrote: > Is there any reason why tuples need to throw an exception on assigning to > the element if the old value and new value are the same object? > > If I say: > > a = ("spam", [10, 30], "eggs") > > then > > a[0] = a[0] > > won't actually mutate the obj

Re: Tuples and immutability

2014-03-07 Thread Alister
On Fri, 07 Mar 2014 09:33:49 +, Duncan Booth wrote: > Chris Angelico wrote: > >> On Sat, Mar 1, 2014 at 1:41 AM, Joshua Landau wrote: >>> Would it be better to add a check here, such that if this gets raised >>> to the top-level it includes a warning ("Addition was inplace; >>> variable pro

Re: Tuples and immutability

2014-03-07 Thread Chris Angelico
On Fri, Mar 7, 2014 at 10:38 PM, Peter Otten <__pete...@web.de> wrote: > TypeError: 257 is not 257 > > I'm not sure "help" is the right word here ;) It doesn't help with non-small integers, yes, but the original case was a list. Personally, I don't think there are many situations that would benefi

Re: Tuples and immutability

2014-03-07 Thread Peter Otten
Chris Angelico wrote: > On Fri, Mar 7, 2014 at 8:33 PM, Duncan Booth > wrote: >> Is there any reason why tuples need to throw an exception on assigning to >> the element if the old value and new value are the same object? > > It'd be easy enough to implement your own tuple subclass that behaves

Re: Tuples and immutability

2014-03-07 Thread Chris Angelico
On Fri, Mar 7, 2014 at 8:33 PM, Duncan Booth wrote: > Is there any reason why tuples need to throw an exception on assigning to > the element if the old value and new value are the same object? It'd be easy enough to implement your own tuple subclass that behaves that way. Try it! See how many si

Re: Tuples and immutability

2014-03-07 Thread Ben Finney
Duncan Booth writes: > Is there any reason why tuples need to throw an exception on assigning > to the element if the old value and new value are the same object? Special cases aren't special enough to break the rules. -- \ “I do not believe in forgiveness as it is preached by the |

Re: Tuples and immutability

2014-03-07 Thread Duncan Booth
Chris Angelico wrote: > On Sat, Mar 1, 2014 at 1:41 AM, Joshua Landau > wrote: >> Would it be better to add a check here, such that if this gets raised >> to the top-level it includes a warning ("Addition was inplace; >> variable probably mutated despite assignment failure")? > > That'd requir

Re: Tuples and immutability

2014-03-02 Thread albert visser
On Sun, 02 Mar 2014 15:17:11 +0100, Eric Jacoboni wrote: Le 02/03/2014 15:05, Mark Lawrence a écrit : The behaviour is consistent except when you try to modify a tuple. Not in my opinion... li = [10, 30] li = li + "spam" --> TypeError: can only concatenate list (not "str") li += "spam

Re: Tuples and immutability

2014-03-02 Thread Eric Jacoboni
Le 02/03/2014 15:05, Mark Lawrence a écrit : > The behaviour is consistent except when you try to modify a tuple. > Not in my opinion... li = [10, 30] li = li + "spam" --> TypeError: can only concatenate list (not "str") li += "spam" --> Ok So, not, that's not what i call consistent.

Re: Tuples and immutability

2014-03-02 Thread Mark Lawrence
On 02/03/2014 13:38, Eric Jacoboni wrote: Le 02/03/2014 13:32, Ian Kelly a écrit : On Sat, Mar 1, 2014 at 7:04 PM, Eric Jacoboni wrote: In fact, i think i'm gonna forget += on lists :) Well, do what you want, but I think you're taking the wrong lesson from this. Don't forget about using +=

Re: Tuples and immutability

2014-03-02 Thread Eric Jacoboni
Le 02/03/2014 13:32, Ian Kelly a écrit : > On Sat, Mar 1, 2014 at 7:04 PM, Eric Jacoboni wrote: >> In fact, i think i'm gonna forget += on lists :) > > Well, do what you want, but I think you're taking the wrong lesson > from this. Don't forget about using += on lists. Instead, forget > about u

Re: Tuples and immutability

2014-03-02 Thread Ian Kelly
On Sat, Mar 1, 2014 at 7:04 PM, Eric Jacoboni wrote: > In fact, i think i'm gonna forget += on lists :) Well, do what you want, but I think you're taking the wrong lesson from this. Don't forget about using += on lists. Instead, forget about using assignments, augmented or otherwise, on tuple e

Re: Tuples and immutability

2014-03-01 Thread Mark H. Harris
On Saturday, March 1, 2014 8:04:32 PM UTC-6, Eric Jacoboni wrote: > > > In fact, i think i'm gonna forget += on lists :) hi Eric, well, that might be extreme, but you certainly want to avoid trying to change an immutable. Something you might want to consider is trying something like creating a n

Re: Tuples and immutability

2014-03-01 Thread Mark H. Harris
On Saturday, March 1, 2014 3:21:44 PM UTC-6, Mark H. Harris wrote: > On Friday, February 28, 2014 11:16:18 PM UTC-6, Ian wrote: hi Ian, I thought of something else after I slept on it, so to speak. Consider this: >>> s=(2,3,[42,43,44],7) >>> s[2] [42, 43, 44] >>> s[2] is a list. We shoul

Re: Tuples and immutability

2014-03-01 Thread Eric Jacoboni
Le 01/03/2014 22:21, Mark H. Harris a écrit : > The point I'm trying to make with this post is that s[2]+=[46] and > s[2]=s[2]+[46] are handled inconsistently. For my own, the fact that, in Python, a_liste += e_elt gives a different result than a_list = a_list + e_elt is a big source of trou

Re: Tuples and immutability

2014-03-01 Thread Mark H. Harris
On Friday, February 28, 2014 11:16:18 PM UTC-6, Ian wrote: > How would you propose doing that? Bear in mind that while Python > knows that tuples specifically are immutable, it doesn't generally > know whether a type is immutable. hi Ian, I thought of something else after I slept on it, so to

Re: Tuples and immutability

2014-03-01 Thread Mark H. Harris
On Saturday, March 1, 2014 8:54:43 AM UTC-6, Mark Lawrence wrote: > you're also using google groups... it doesn't wrap paragraphs > > correctly... please read and action this > > https://wiki.python.org/moin/GoogleGroupsPython... it does wrap > > paragraphs correctly... it also prevents

Re: Tuples and immutability

2014-03-01 Thread Ian Kelly
On Fri, Feb 28, 2014 at 11:25 PM, Mark H. Harris wrote: > On Friday, February 28, 2014 11:16:18 PM UTC-6, Ian wrote: > >> How would you propose doing that? Bear in mind that while Python >> knows that tuples specifically are immutable, it doesn't generally >> know whether a type is immutable. > >

Re: Tuples and immutability

2014-03-01 Thread Oscar Benjamin
On 27 February 2014 21:47, Nick Timkovich wrote: > On Thu, Feb 27, 2014 at 10:33 AM, Chris Angelico wrote: >> >> It's unintuitive, but it's a consequence of the way += is defined. If >> you don't want assignment, don't use assignment :) > > Where is `.__iadd__()` called outside of `list += X`? N

Re: Tuples and immutability

2014-03-01 Thread Ned Batchelder
On 3/1/14 12:50 AM, Mark H. Harris wrote: On Friday, February 28, 2014 11:34:56 PM UTC-6, Ian wrote: One very common example of tuples containing lists is when lists are passed to any function that accepts *args, because the extra arguments are passed in a tuple. A similarly common example is

Re: Tuples and immutability

2014-03-01 Thread Mark Lawrence
On 01/03/2014 06:16, Mark H. Harris wrote: On Friday, February 28, 2014 11:16:18 PM UTC-6, Ian wrote: How would you propose doing that? Bear in mind that while Python knows that tuples specifically are immutable, it doesn't generally know whether a type is immutable. hi Ian, consider the

Re: Tuples and immutability

2014-02-28 Thread Mark H. Harris
On Friday, February 28, 2014 11:16:18 PM UTC-6, Ian wrote: > How would you propose doing that? Bear in mind that while Python > knows that tuples specifically are immutable, it doesn't generally > know whether a type is immutable. I was going to bed, and then thought of the solution. Allow th

Re: Tuples and immutability

2014-02-28 Thread Chris Angelico
On Sat, Mar 1, 2014 at 5:16 PM, Mark H. Harris wrote: > This is what I mean... the error message is telling the user that it cannot > do what he has requested, and yet IT DID. You have one of two scenarios: > 1) the message is arbitrarily lying and it really can assign an immutable's > ite

Re: Tuples and immutability

2014-02-28 Thread Mark H. Harris
On Friday, February 28, 2014 11:16:18 PM UTC-6, Ian wrote: > How would you propose doing that? Bear in mind that while Python > knows that tuples specifically are immutable, it doesn't generally > know whether a type is immutable. hi Ian, consider the problem... its a "cake" and "eat it too

Re: Tuples and immutability

2014-02-28 Thread Mark H. Harris
On Friday, February 28, 2014 11:34:56 PM UTC-6, Ian wrote: > One very common example of tuples containing lists is when lists are > passed to any function that accepts *args, because the extra arguments > are passed in a tuple. A similarly common example is when returning > multiple objects from

Re: Tuples and immutability

2014-02-28 Thread Ian Kelly
On Fri, Feb 28, 2014 at 9:45 PM, Mark H. Harris wrote: > I really believe IMHO that the error should have come when you made the list > an item of a tuple. An immutable object should have NO REASON to contain a > mutable object like list... I mean the whole point is to eliminate the > overhea

Re: Tuples and immutability

2014-02-28 Thread Ian Kelly
On Fri, Feb 28, 2014 at 6:27 PM, Eric Jacoboni wrote: > Anyway, the TypeError should rollback, not commit the mistake. The Python interpreter isn't a database. It can't rollback the object because the operation that was performed may not be reversible. Consider for example a socket class where t

Re: Tuples and immutability

2014-02-28 Thread Ian Kelly
On Fri, Feb 28, 2014 at 5:22 PM, Mark H. Harris wrote: > I really think this is a bug; honestly. IMHO it should be an error to use > += with an immutable type and that means not at all. In other words, the > list should not even be considered, because we're talking about changing a > tuple

Re: Tuples and immutability

2014-02-28 Thread Mark H. Harris
On Friday, February 28, 2014 7:27:17 PM UTC-6, Eric Jacoboni wrote: > I agree with that too... My error was to first consider the list, then > the tuple... I should have considered the tuple first... > Anyway, the TypeError should rollback, not commit the mistake. I believe so too, but I'm not o

Re: Tuples and immutability

2014-02-28 Thread Chris Angelico
On Sat, Mar 1, 2014 at 11:22 AM, Mark H. Harris wrote: > lists within a tuple should be converted to tuples.If you want a tuple to > hold a list, make it a list in the first place. Tuples should not be > changed... and as you point out... half changing a tuple is not a good > condition if

Re: Tuples and immutability

2014-02-28 Thread Eric Jacoboni
Le 01/03/2014 01:22, Mark H. Harris a écrit : > I'll address the second first by asking a question... should an immutable > type (object) be able to hold (contain) mutable objects ... should tuples be > allowed to hold lists? > > lists within a tuple should be converted to tuples.If you w

Re: Tuples and immutability

2014-02-28 Thread Mark H. Harris
On Thursday, February 27, 2014 10:01:39 AM UTC-6, Eric Jacoboni wrote: > > ('spam', [10, 30, 20], 'eggs') > > I get a TypeError for an illegal operation, but this operation is still > > completed? hi Eric, others have answered your question specifically, but the issue (which is recurring) b

Re: Tuples and immutability

2014-02-28 Thread Chris Angelico
On Sat, Mar 1, 2014 at 1:41 AM, Joshua Landau wrote: > Would it be better to add a check here, such that if this gets raised > to the top-level it includes a warning ("Addition was inplace; > variable probably mutated despite assignment failure")? That'd require figuring out whether or not the va

Re: Tuples and immutability

2014-02-28 Thread Joshua Landau
On 27 February 2014 16:14, Chris Angelico wrote: > On Fri, Feb 28, 2014 at 3:01 AM, Eric Jacoboni > wrote: >> > a_tuple = ("spam", [10, 30], "eggs") > a_tuple[1] += [20] >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: 'tuple' object does not support item ass

Re: Tuples and immutability

2014-02-27 Thread Marko Rauhamaa
John O'Hagan : > Same object, just a different name - but a different result. I get > why, but still find that odd. The general principle is stated in the language specification: http://docs.python.org/3.2/reference/simple_stmts.html #augmented-assignment-statements>: Also, when possib

Re: Tuples and immutability

2014-02-27 Thread John O'Hagan
On Thu, 27 Feb 2014 18:19:09 +0200 Marko Rauhamaa wrote: > Eric Jacoboni : > > a_tuple[1] += [20] > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: 'tuple' object does not support item assignment > > > > [...] > > > > But, then, why a_tuple is still modified?

Re: Tuples and immutability

2014-02-27 Thread Ian Kelly
On Thu, Feb 27, 2014 at 2:47 PM, Nick Timkovich wrote: > On Thu, Feb 27, 2014 at 10:33 AM, Chris Angelico wrote: >> >> On Fri, Feb 28, 2014 at 3:27 AM, Eric Jacoboni >> wrote: >> > But, imho, it's far from being a intuitive result, to say the least. >> >> It's unintuitive, but it's a consequence

Re: Tuples and immutability

2014-02-27 Thread Chris Angelico
On Fri, Feb 28, 2014 at 8:47 AM, Nick Timkovich wrote: > On Thu, Feb 27, 2014 at 10:33 AM, Chris Angelico wrote: >> >> On Fri, Feb 28, 2014 at 3:27 AM, Eric Jacoboni >> wrote: >> > But, imho, it's far from being a intuitive result, to say the least. >> >> It's unintuitive, but it's a consequence

Re: Tuples and immutability

2014-02-27 Thread Nick Timkovich
On Thu, Feb 27, 2014 at 10:33 AM, Chris Angelico wrote: > On Fri, Feb 28, 2014 at 3:27 AM, Eric Jacoboni > wrote: > > But, imho, it's far from being a intuitive result, to say the least. > > It's unintuitive, but it's a consequence of the way += is defined. If > you don't want assignment, don't

Re: Tuples and immutability

2014-02-27 Thread Zachary Ware
On Thu, Feb 27, 2014 at 10:27 AM, Eric Jacoboni wrote: > Le 27/02/2014 17:13, Zachary Ware a écrit : >> >> You're not the first person to have this question :) >> >> http://docs.python.org/3/faq/programming.html#why-does-a-tuple-i-item-raise-an-exception-when-the-addition-works >> > > Oh yes, i wa

Re: Tuples and immutability

2014-02-27 Thread Chris Angelico
On Fri, Feb 28, 2014 at 3:27 AM, Eric Jacoboni wrote: > But, imho, it's far from being a intuitive result, to say the least. It's unintuitive, but it's a consequence of the way += is defined. If you don't want assignment, don't use assignment :) ChrisA -- https://mail.python.org/mailman/listinf

Re: Tuples and immutability

2014-02-27 Thread Eric Jacoboni
Le 27/02/2014 17:13, Zachary Ware a écrit : > > You're not the first person to have this question :) > > http://docs.python.org/3/faq/programming.html#why-does-a-tuple-i-item-raise-an-exception-when-the-addition-works > Oh yes, i was aware of this explanation (thanks to Chris for his answer, too

Re: Tuples and immutability

2014-02-27 Thread Marko Rauhamaa
Eric Jacoboni : a_tuple[1] += [20] > Traceback (most recent call last): > File "", line 1, in > TypeError: 'tuple' object does not support item assignment > > [...] > > But, then, why a_tuple is still modified? That's because the += operator 1. modifies the list object in place 2. tri

Re: Tuples and immutability

2014-02-27 Thread Chris Angelico
On Fri, Feb 28, 2014 at 3:01 AM, Eric Jacoboni wrote: > I'm using Python 3.3 and i have a problem for which i've still not found > any reasonable explanation... > a_tuple = ("spam", [10, 30], "eggs") a_tuple[1] += [20] > Traceback (most recent call last): > File "", line 1, in > TypeE

Re: Tuples and immutability

2014-02-27 Thread Zachary Ware
On Thu, Feb 27, 2014 at 10:01 AM, Eric Jacoboni wrote: > Hi, > > I'm using Python 3.3 and i have a problem for which i've still not found > any reasonable explanation... > a_tuple = ("spam", [10, 30], "eggs") a_tuple[1] += [20] > Traceback (most recent call last): > File "", line 1, in

Re: Tuples vs. variable-length argument lists

2010-03-22 Thread Jean-Michel Pichavant
Spencer Pearson wrote: Hi! This might be more of a personal-preference question than anything, but here goes: when is it appropriate for a function to take a list or tuple as input, and when should it allow a varying number of arguments? It seems as though the two are always interchangeable. For

Re: Tuples vs. variable-length argument lists

2010-03-19 Thread Steven D'Aprano
On Fri, 19 Mar 2010 17:20:31 -0700, Spencer Pearson wrote: > Hi! > > This might be more of a personal-preference question than anything, but > here goes: when is it appropriate for a function to take a list or tuple > as input, and when should it allow a varying number of arguments? That depend

Re: Tuples part 2

2008-06-05 Thread Ivan Illarionov
On 5 июн, 21:22, George Sakkis <[EMAIL PROTECTED]> wrote: > On Jun 5, 11:48 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > > On 5 июн, 19:38, George Sakkis <[EMAIL PROTECTED]> wrote: > > > > On Jun 5, 11:21 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > > On 5 июн, 18:56, Ivan Illar

Re: Tuples part 2

2008-06-05 Thread George Sakkis
On Jun 5, 11:48 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > On 5 июн, 19:38, George Sakkis <[EMAIL PROTECTED]> wrote: > > > > > On Jun 5, 11:21 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > On 5 июн, 18:56, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > > On 5 июн, 18:19, "[EMAIL

Re: Tuples part 2

2008-06-05 Thread Ivan Illarionov
On 5 июн, 19:38, George Sakkis <[EMAIL PROTECTED]> wrote: > On Jun 5, 11:21 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > > On 5 июн, 18:56, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > On 5 июн, 18:19, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > > wrote: > > > > > On Jun 5, 3:49 pm,

Re: Tuples part 2

2008-06-05 Thread George Sakkis
On Jun 5, 11:21 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > On 5 июн, 18:56, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > > On 5 июн, 18:19, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > wrote: > > > > On Jun 5, 3:49 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > > On 5 ÉÀÎ, 01:57,

Re: Tuples part 2

2008-06-05 Thread Ivan Illarionov
On 5 июн, 18:56, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > On 5 июн, 18:19, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote: > > > > > On Jun 5, 3:49 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > On 5 ÉÀÎ, 01:57, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > > wrote: > > > > > Hi Everyone

Re: Tuples part 2

2008-06-05 Thread Ivan Illarionov
On 5 июн, 18:19, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Jun 5, 3:49 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > > On 5 ÉÀÎ, 01:57, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > wrote: > > > > Hi Everyone, > > > > i have another question. What if i wanted to make n tuples, each

Re: Tuples part 2

2008-06-05 Thread Ivan Illarionov
On 5 июн, 18:19, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Jun 5, 3:49 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > > > > On 5 ÉÀÎ, 01:57, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > wrote: > > > > Hi Everyone, > > > > i have another question. What if i wanted to make n tuples, each

Re: Tuples part 2

2008-06-05 Thread [EMAIL PROTECTED]
On Jun 5, 3:49 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > On 5 ÉÀÎ, 01:57, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote: > > > > > Hi Everyone, > > > i have another question. What if i wanted to make n tuples, each with > > a list of coordinates. For example : > > > coords = list() > > for

Re: Tuples part 2

2008-06-05 Thread Ivan Illarionov
On 5 июн, 01:57, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Hi Everyone, > > i have another question. What if i wanted to make n tuples, each with > a list of coordinates. For example : > > coords = list() > for h in xrange(1,11,1): >for i in xrange(1, 5, 1) : > for j in xrange(1, 5

Re: Tuples part 2

2008-06-05 Thread George Sakkis
On Jun 5, 9:26 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Jun 5, 1:37 am, [EMAIL PROTECTED] wrote: > > > > > [EMAIL PROTECTED]: > > > Do you mean something like this? (notice the many formatting > > differences, use a formatting similar to this one in your code) > > > coords = [] > > >

  1   2   3   >