Re: Dataclasses, immutability(?), and ChatGPT

2023-04-12 Thread Thomas Passin
e, but further attributes can be dynamically-added to the object-instance! Yes, if the code included: coordinates = Coordinates( 5, 6, ) the new "coordinates" identifier would point to a different id() 'address', ie a fresh immutable-instance. The 'book of word

Re: Dataclasses, immutability(?), and ChatGPT

2023-04-12 Thread Grant Edwards
On 2023-04-12, Roel Schroeven wrote: >> Huh? If we'd been discussing namedtuples over (say) dictionaries, I'd >> perhaps have accepted the reply. > > ChatGPT is wrong. > >> Anything I've 'missed'? >> - or a salutary tale of not depending upon ChatGPT etc? > You didn't miss anything, ChatGPT is

Re: Dataclasses, immutability(?), and ChatGPT

2023-04-12 Thread Roel Schroeven
Op 12/04/2023 om 6:58 schreef dn via Python-list: Are dataclasses (or instances thereof) mutable or immutable? - and in what sense? Instances of dataclasses are mutable, just like normal classes. Dataclasses *are* normal classes, with some extra special methods. They are totally different from

Dataclasses, immutability(?), and ChatGPT

2023-04-11 Thread dn via Python-list
output: Commencing execution Coordinates(x=1, y=2) 140436963150928 Coordinates(x=3, y=2) 140436963150928 Coordinates(x=3, y=2) 140436963150928 3 2 4 Terminating ### Not only are a dataclass instance's attribute-values mutable, but further attributes can be dynamically-added to the obje

Re: Clarification on Immutability please

2020-01-28 Thread Chris Angelico
On Wed, Jan 29, 2020 at 1:50 AM Random832 wrote: > > On Wed, Jan 22, 2020, at 02:34, Stephen Tucker wrote: > > Oh dear, I am sorry. I have created quite a storm. > > > > Moreover, I am sorry because I misremembered what I had typed into Idle. My > > original tuple only had two elements, not three,

Re: Clarification on Immutability please

2020-01-28 Thread Random832
On Wed, Jan 22, 2020, at 02:34, Stephen Tucker wrote: > Oh dear, I am sorry. I have created quite a storm. > > Moreover, I am sorry because I misremembered what I had typed into Idle. My > original tuple only had two elements, not three, so the slicing [:2] didn't > affect the tuple at all - and s

Re: Clarification on Immutability please

2020-01-28 Thread Chris Angelico
On Tue, Jan 28, 2020 at 9:33 PM Daniel Haude wrote: > > Am 27.01.2020 15:23 schrieb Chris Angelico: > > > The way execution works in Python, you first evaluate the object, then > > assign it to the target. The new object HAS to exist before the old > > one is replaced. There's no such thing as "at

Re: Clarification on Immutability please

2020-01-28 Thread Daniel Haude
Am 27.01.2020 15:23 schrieb Chris Angelico: The way execution works in Python, you first evaluate the object, then assign it to the target. The new object HAS to exist before the old one is replaced. There's no such thing as "atomic reassignment" that simultaneously destroys the old object and a

Re: Clarification on Immutability please

2020-01-27 Thread Chris Angelico
On Tue, Jan 28, 2020 at 2:44 AM Souvik Dutta wrote: > > If the two objects have the same value that means that when called all of > them will point to the same object rather than creating the same value - > object pairs twice. Immutability is the characteristics that does not let y

Re: Clarification on Immutability please

2020-01-27 Thread Chris Angelico
On Tue, Jan 28, 2020 at 1:13 AM Musbur wrote: > > Am 21.01.2020 19:38 schrieb Chris Angelico: > > Are you sure that it does? I can't reproduce this. When you slice the > > first two from a tuple, you create a new tuple, and until the > > assignment happens, both the new one and the original coexis

Re: Clarification on Immutability please

2020-01-27 Thread Musbur
Am 21.01.2020 19:38 schrieb Chris Angelico: On Wed, Jan 22, 2020 at 4:42 AM Stephen Tucker wrote: and even that the first id(mytup) returns the same address as the second one, I am left wondering exactly what immutability is. Let's look at id()'s documentation: id(object)

Re: Clarification on Immutability please

2020-01-22 Thread Richard Damon
On 1/21/20 8:41 PM, Jon Ribbens via Python-list wrote: Whether we call the link between 'foo' and (1, 2) a pointer or a reference is almost entirely irrelevant, the difference is essentially meaningless. The issue is that in Python, objects and names are fairly distinct. Unlike a language like

Re: Clarification on Immutability please

2020-01-22 Thread Eryk Sun
On 1/21/20, Jon Ribbens via Python-list wrote: > > Whether we call the link between 'foo' and (1, 2) a pointer or a > reference is almost entirely irrelevant, the difference is essentially > meaningless. In programming terms, a "pointer" is always an address in memory, and, at least to me, the te

Re: Clarification on Immutability please

2020-01-21 Thread Stephen Tucker
Oh dear, I am sorry. I have created quite a storm. Moreover, I am sorry because I misremembered what I had typed into Idle. My original tuple only had two elements, not three, so the slicing [:2] didn't affect the tuple at all - and so the second id() gave the same address as the first one. So, y

Re: Clarification on Immutability please

2020-01-21 Thread Michael Torrie
On 1/21/20 6:52 PM, Ethan Furman wrote: > On 01/21/2020 10:55 AM, Michael Torrie wrote: > >> Slicing >> returns a new object whether one is slicing a tuple, list, or a string, >> the latter two are mutable objects. > > Strings are not mutable. Yup I got my items in the wrong order. I meant to sa

Re: Clarification on Immutability please

2020-01-21 Thread MRAB
On 2020-01-22 01:52, Ethan Furman wrote: On 01/21/2020 10:55 AM, Michael Torrie wrote: Slicing returns a new object whether one is slicing a tuple, list, or a string, the latter two are mutable objects. Strings are not mutable. In the case of tuples and strings, if the slicing encompasses th

Re: Clarification on Immutability please

2020-01-21 Thread Ethan Furman
On 01/21/2020 10:55 AM, Michael Torrie wrote: Slicing returns a new object whether one is slicing a tuple, list, or a string, the latter two are mutable objects. Strings are not mutable. -- ~Ethan~ -- https://mail.python.org/mailman/listinfo/python-list

Re: Clarification on Immutability please

2020-01-21 Thread Jon Ribbens via Python-list
On 2020-01-21, Chris Angelico wrote: > On Wed, Jan 22, 2020 at 8:01 AM Jon Ribbens via Python-list > wrote: >> On 2020-01-21, Chris Angelico wrote: >> > On Wed, Jan 22, 2020 at 4:42 AM Stephen Tucker >> > wrote: >> >> I am left concluding that mytup is not actually a tuple (even though type >>

Re: Clarification on Immutability please

2020-01-21 Thread Cameron Simpson
On 22Jan2020 09:15, Cameron Simpson wrote: It doesn't say anything about the contents of the internal objects: >>> mytup = (1, [2, 3], (4, 5)) [...] >>> mytupl[1] = [7, 8] Traceback (most recent call last): File "", line 1, in NameError: name 'mytupl' is not defined Please igno

Re: Clarification on Immutability please

2020-01-21 Thread Cameron Simpson
ht", "credits" or "license" for more information. >>> mytup = ("q", "w", "e") >>> id(mytup) 4541315104 >>> mytup = mytup [:2] >>> id(mytup) 4542334880 >>> I am left wondering exactly what

Re: Clarification on Immutability please

2020-01-21 Thread Chris Angelico
On Wed, Jan 22, 2020 at 8:01 AM Jon Ribbens via Python-list wrote: > > On 2020-01-21, Chris Angelico wrote: > > On Wed, Jan 22, 2020 at 4:42 AM Stephen Tucker > > wrote: > >> I am left concluding that mytup is not actually a tuple (even though type > >> (mytup) tells me that it is). > > > > If

Re: Clarification on Immutability please

2020-01-21 Thread Jon Ribbens via Python-list
On 2020-01-21, Chris Angelico wrote: > On Wed, Jan 22, 2020 at 4:42 AM Stephen Tucker wrote: >> I am left concluding that mytup is not actually a tuple (even though type >> (mytup) tells me that it is). > > If type(mytup) is tuple, then mytup really truly is a tuple. There is > no other conclusio

Re: Clarification on Immutability please

2020-01-21 Thread Chris Angelico
ens, both the new one and the original coexist, which > > means they MUST have unique IDs. > > And furthermore this has nothing to do with immutability. Slicing > returns a new object whether one is slicing a tuple, list, or a string, > the latter two are mutable objects. True, but th

Re: Clarification on Immutability please

2020-01-21 Thread Michael Torrie
unique IDs. And furthermore this has nothing to do with immutability. Slicing returns a new object whether one is slicing a tuple, list, or a string, the latter two are mutable objects. -- https://mail.python.org/mailman/listinfo/python-list

Re: Clarification on Immutability please

2020-01-21 Thread Chris Angelico
", "w", "e") > id(mytup) > mytup = mytup [:2] > id(mytup) > > and even that the first id(mytup) returns the same address as the second > one, I am left wondering exactly what immutability is. Are you sure that it does? I can't reproduce this. When you sl

Clarification on Immutability please

2020-01-21 Thread Stephen Tucker
rst id(mytup) returns the same address as the second one, I am left wondering exactly what immutability is. I am left concluding that mytup is not actually a tuple (even though type (mytup) tells me that it is). My only explanation is that mytup is, actually, a pointer to a tuple; the pointer can

Re: immutability is not strictly the same as having an unchangeable value, it is more subtle

2019-04-18 Thread Chris Angelico
On Thu, Apr 18, 2019 at 6:16 PM Gregory Ewing wrote: > > Arup Rakshit wrote: > > What protocols I need to > > learn, to define a custom immutable class ? > > That depends on how strictly you want to enforce immutability. > > The easiest thing is not to enforce it at

Re: immutability is not strictly the same as having an unchangeable value, it is more subtle

2019-04-18 Thread Gregory Ewing
Arup Rakshit wrote: What protocols I need to learn, to define a custom immutable class ? That depends on how strictly you want to enforce immutability. The easiest thing is not to enforce it at all and simply refrain from mutating it. This is very often done. You can provide some protection

Re: immutability is not strictly the same as having an unchangeable value, it is more subtle

2019-04-17 Thread Arup Rakshit
Hi All, Thanks for explaining it so nice way. I got it now. What protocols I need to learn, to define a custom immutable class ? Thanks, Arup Rakshit a...@zeit.io > On 16-Apr-2019, at 10:54 PM, Dennis Lee Bieber wrote: > > On Tue, 16 Apr 2019 13:13:18 +0530, Arup Rakshit declaimed the >

Re: immutability is not strictly the same as having an unchangeable value, it is more subtle

2019-04-16 Thread Chris Angelico
hat contains a reference to a mutable object can change when the latter’s > > value is changed; however the container is still considered immutable, > > because the collection of objects it contains cannot be changed. So, > > immutability is not strictly the same as having an unch

immutability is not strictly the same as having an unchangeable value, it is more subtle

2019-04-16 Thread Arup Rakshit
tainer is still considered immutable, because the collection > of objects it contains cannot be changed. So, immutability is not strictly > the same as having an unchangeable value, it is more subtle.) An object’s > mutability is determined by its type; for instance, numbers, strings and

Re: Question about implementing immutability

2018-11-22 Thread Iwo Herka
czw., 22 lis 2018 o 11:14 Thomas Jollans napisał(a): > [..] this allows other classes' __init__s to set attributes. Fair point. > I might try setting a self._fixed flag at the end of init and do a check > > if getattr(self, '_fixed', False): > raise TypeError(f"'{type(self)}' is immutable")

Re: Question about implementing immutability

2018-11-22 Thread Thomas Jollans
On 2018-11-21 17:45, Iwo Herka wrote: > Hello, > > Let's say I want to implement immutability for user-defined class. > More precisely, a class that can be modified only in its (or its > super-class') __init__ method. My initial idea was to do it the > following fashi

Re: Question about implementing immutability

2018-11-22 Thread Iwo Herka
czw., 22 lis 2018 o 10:53 Thomas Jollans napisał(a): > If you're tempted to go down that route and can require Python 3.7, use > dataclasses! I'm aware of them, thanks. :) Dataclasses are great for certain use-cases; I was just wondering how hard would it be to implement something approximating g

Re: Question about implementing immutability

2018-11-22 Thread Thomas Jollans
gt;> Hello, >> >> Let's say I want to implement immutability for user-defined class. >> More precisely, a class that can be modified only in its (or its >> super-class') __init__ method. My initial idea was to do it the >> following fashion: >> >

Re: Question about implementing immutability

2018-11-22 Thread Iwo Herka
Dan Sommers <2qdxy4rzwzuui...@potatochowder.com> wrote: > If an instance of your class contains a list, and you change one > of the elements of that list, then the instance's __setattr__ > never comes into play: I think that's within the bounds of what is understood as "immutable" in Python. Tuple

Re: Question about implementing immutability

2018-11-21 Thread dieter
Iwo Herka writes: > Let's say I want to implement immutability for user-defined class. > More precisely, a class that can be modified only in its (or its > super-class') __init__ method. My initial idea was to do it the > following fashion: > > def __set

Re: Question about implementing immutability

2018-11-21 Thread Calvin Spealman
If you want to create your own immutable class, maybe inherit from a namedtuple? On Wed, Nov 21, 2018 at 11:45 AM Iwo Herka wrote: > Hello, > > Let's say I want to implement immutability for user-defined class. > More precisely, a class that can be modified only in its (or

Re: Question about implementing immutability

2018-11-21 Thread Dan Sommers
On 11/21/18 11:45 AM, Iwo Herka wrote: Hello, Let's say I want to implement immutability for user-defined class. More precisely, a class that can be modified only in its (or its super-class') __init__ method. My initial idea was to do it the following fashion: def __setattr__(s

Question about implementing immutability

2018-11-21 Thread Iwo Herka
Hello, Let's say I want to implement immutability for user-defined class. More precisely, a class that can be modified only in its (or its super-class') __init__ method. My initial idea was to do it the following fashion: def __setattr__(self, *args, **kwargs): if sys._

Re: Immutability of Floats, Ints and Strings in Python

2016-11-28 Thread Random832
On Fri, Nov 25, 2016, at 06:33, Ned Batchelder wrote: > A Python implementation can choose when to reuse immutable objects and > when not to. Reusing a value has a cost, because the values have to > be kept, and then found again. So the cost is only paid when there's > a reasonable chance that the

Re: Immutability of Floats, Ints and Strings in Python

2016-11-25 Thread Steve D'Aprano
On Fri, 25 Nov 2016 10:37 pm, Ned Batchelder wrote: > And: floats are rarely checked for equality, and very very very rarely > used as dict keys, so there's no gain by short-circuiting the equality > check. You cannot short-circuit the equality check, at least not without giving up IEEE-754 seman

Re: Immutability of Floats, Ints and Strings in Python

2016-11-25 Thread Ned Batchelder
On Friday, November 25, 2016 at 7:17:08 AM UTC-5, BartC wrote: > On 25/11/2016 11:24, Nikunj wrote: > > > > Out of curiosity, I wanted to understand the reason behind having different > > memory location for two identical floats . This is unlike ints or strings. > > Tried googling but couldn't fi

Re: Immutability of Floats, Ints and Strings in Python

2016-11-25 Thread BartC
On 25/11/2016 11:24, Nikunj wrote: Out of curiosity, I wanted to understand the reason behind having different memory location for two identical floats . This is unlike ints or strings. Tried googling but couldn't find anything concrete. Any links or references would be appreciated! Do you

Re: Immutability of Floats, Ints and Strings in Python

2016-11-25 Thread Ned Batchelder
On Friday, November 25, 2016 at 6:34:00 AM UTC-5, Ned Batchelder wrote: > On Friday, November 25, 2016 at 6:24:47 AM UTC-5, Nikunj wrote: > > Hi All, > > > > Out of curiosity, I wanted to understand the reason behind having different > > memory location for two identical floats . This is unlike i

Re: Immutability of Floats, Ints and Strings in Python

2016-11-25 Thread Ned Batchelder
On Friday, November 25, 2016 at 6:24:47 AM UTC-5, Nikunj wrote: > Hi All, > > Out of curiosity, I wanted to understand the reason behind having different > memory location for two identical floats . This is unlike ints or strings. > Tried googling but couldn't find anything concrete. Any links o

Immutability of Floats, Ints and Strings in Python

2016-11-25 Thread Nikunj
Hi All, Out of curiosity, I wanted to understand the reason behind having different memory location for two identical floats . This is unlike ints or strings. Tried googling but couldn't find anything concrete. Any links or references would be appreciated! Example: For FLOATS: == >>>

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
interface of the >> class, not the implementation. >> >> Would you say that whether list.append operates in place or creates a >> new list is an implementation detail? Whether str.upper() creates a new >> string or modifies the existing one in place? > > Of course no

Re: Tuples and immutability

2014-03-10 Thread Ian Kelly
whether list.append operates in place or creates a new > list is an implementation detail? Whether str.upper() creates a new > string or modifies the existing one in place? Of course not. list.append is documented as modifying the list. str.upper is documented as returning a copy of the stri

Re: Tuples and immutability

2014-03-09 Thread Steven D'Aprano
u say that whether list.append operates in place or creates a new list is an implementation detail? Whether str.upper() creates a new string or modifies the existing one in place? Mutability versus immutability is part of the interface, not implementation, not withstanding that

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

  1   2   3   >