Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Michel Desmoulin
To void introducing a new built-in, we could do object.bag = SimpleNamespace Le 27/07/2017 à 05:23, Mike Miller a écrit : > Many times in the olden days when I needed a bag o' attributes to be > passed around like a struct I'd make a dummy class, then instantiate > it. (A lot harder than the java

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Nick Coghlan
On 27 July 2017 at 03:10, Steven D'Aprano wrote: > On Thu, Jul 27, 2017 at 02:05:47AM +1000, Nick Coghlan wrote: >> On 26 July 2017 at 11:05, Steven D'Aprano wrote: >> > I don't see any way that this proposal can be anything by a subtle >> > source of bugs. We have two *incompatible* requirements

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Nick Coghlan
On 27 July 2017 at 10:38, Steven D'Aprano wrote: > On Thu, Jul 27, 2017 at 11:46:45AM +1200, Greg Ewing wrote: >> Nick Coghlan wrote: >> >The same applies to the ntuple concept, expect there it's the fact >> >that it's a *tuple* that conveys the "order matters" expectation. >> >> That assumes ther

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Chris Barker
> To avoid introducing a new built-in, we could do object.bag = SimpleNamespace I am liking the idea of making SimpleNamespace more accessible, but maybe we need to think a bit more about why one might want a tuple-with-names, rather than just an easy way to create an object-with-just-attributes.

[Python-ideas] namedtuple nit...

2017-07-27 Thread Chris Barker
Since we are talking about namedtuple and implementation, I just noticed: In [22]: Point = namedtuple('Point', ['x', 'y']) In [23]: p = Point(2,3) In [24]: p.x = 5 --- AttributeErrorTraceback (most

Re: [Python-ideas] namedtuple nit...

2017-07-27 Thread Ivan Levkivskyi
This error message is the same for types with __slots__, and probably it is indeed a bit too terse. -- Ivan On 27 July 2017 at 18:26, Chris Barker wrote: > Since we are talking about namedtuple and implementation, I just noticed: > > In [22]: Point = namedtuple('Point', ['x', 'y']) > In [23]:

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Pavol Lisy
On 7/26/17, Steven D'Aprano wrote: [...] > But this has a hidden landmine. If *any* module happens to use ntuple > with the same field names as you, but in a different order, you will > have mysterious bugs: > > x, y, z = spam > > You expect x=2, y=1, z=0 because that's the way you defined the f

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Chris Angelico
On Fri, Jul 28, 2017 at 7:22 AM, Pavol Lisy wrote: > On 7/26/17, Steven D'Aprano wrote: > > [...] > >> But this has a hidden landmine. If *any* module happens to use ntuple >> with the same field names as you, but in a different order, you will >> have mysterious bugs: >> >> x, y, z = spam >> >>

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Markus Meskanen
> But you'd need to decide whether you want attributes (spam.x, spam.y) or items (spam["x"], spam["y"]). Both would be useful at different times. ChrisA If something like this was ever added, it'd probably be items, then you could implement a custom __unpack__ (or whatever name it'd be) method

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread אלעזר
On Fri, Jul 28, 2017 at 12:51 AM Chris Angelico wrote: > On Fri, Jul 28, 2017 at 7:22 AM, Pavol Lisy wrote: > > > We have: > > from module import x, y, z # where order is not important > > > > Could we have something similar with ntuple (SimpleNamespace, ...)? > > > > maybe: > > for x, y, z fro

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Chris Barker
On Thu, Jul 27, 2017 at 2:50 PM, Chris Angelico wrote: > On Fri, Jul 28, 2017 at 7:22 AM, Pavol Lisy wrote: > > maybe: > > for x, y, z from spam: > > print(x, y, z) > > What you're asking for is something like JavaScript's "object > destructuring" syntax. Wasn't there just a big long discu

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Chris Angelico
On Fri, Jul 28, 2017 at 9:31 AM, Chris Barker wrote: > > > On Thu, Jul 27, 2017 at 2:50 PM, Chris Angelico wrote: >> >> On Fri, Jul 28, 2017 at 7:22 AM, Pavol Lisy wrote: >> > maybe: >> > for x, y, z from spam: >> > print(x, y, z) >> >> What you're asking for is something like JavaScript's "

Re: [Python-ideas] namedtuple nit...

2017-07-27 Thread Mike Miller
I've never liked that error message either: >>> object().foo = 'bar' Traceback (most recent call last): File "", line 1, in AttributeError: 'object' object has no attribute 'foo' Should say the "object is immutable," not writable, or something of the sort. On 2017-07-27 09:

Re: [Python-ideas] namedtuple nit...

2017-07-27 Thread Chris Angelico
On Fri, Jul 28, 2017 at 10:09 AM, Mike Miller wrote: > I've never liked that error message either: > > >>> object().foo = 'bar' > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'object' object has no attribute 'foo' > > > Should say the "object is imm

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Stephen J. Turnbull
MRAB writes: > But what about: > > >>> nt2 = ntuple(y=2, x=1) > > ? Does that mean that nt[0] == 2? Presumably, yes. > > Does nt == nt2? > > If it's False, then you've lost some of the advantage of using names > instead of positions. Sure. And if you use a dict, you've lost some

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Stephen J. Turnbull
Ethan Furman writes: > Tuples, named or otherwise, are positional first -- order matters. > Specifying > > point = ntuple(y=2, x=-3) > > and having point[0] == 3 is going to be bizarre. This will be a > source for horrible bugs. I don't see how you get that? Anyway, I expect that

Re: [Python-ideas] namedtuple nit...

2017-07-27 Thread Steven D'Aprano
On Thu, Jul 27, 2017 at 05:09:56PM -0700, Mike Miller wrote: > I've never liked that error message either: > > >>> object().foo = 'bar' > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'object' object has no attribute 'foo' > > > Should say the "obj

Re: [Python-ideas] namedtuple literals [Was: RE a new namedtuple]

2017-07-27 Thread Ethan Furman
On 07/27/2017 06:24 PM, Stephen J. Turnbull wrote: Ethan Furman writes: Tuples, named or otherwise, are positional first -- order matters. Specifying point = ntuple(y=2, x=-3) and having point[0] == 3 is going to be bizarre. This will be a source for horrible bugs. I don't see how yo