[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Guido van Rossum
On Fri, Sep 27, 2019 at 6:41 PM Ricky Teachey wrote: > Here's an idea I was toying with in thinking about the problem this > evening. > > Currently, python complains if you try to add a class member that will > conflict with a slot: > > >>> class C: > ... __slots__="x" > ... x=1 > ... >

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Guido van Rossum
On Fri, Sep 27, 2019 at 8:01 PM MRAB wrote: > I was also thinking about suggesting None, but I was wondering whether > that could be misleading because it reads like "no slots". > > What about "..." instead? Would that read better? > > class Point: > __slots__ = > x: float >

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Guido van Rossum
On Fri, Sep 27, 2019 at 7:12 PM Eric V. Smith wrote: > > On Sep 27, 2019, at 8:23 PM, Guido van Rossum wrote: > >  > On Fri, Sep 27, 2019 at 11:18 AM Serhiy Storchaka > wrote: > >> I think it needs an explicit support in the type creation machinery (as >> __slots__ itself has) to support

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread MRAB
On 2019-09-28 03:12, Eric V. Smith wrote: On Sep 27, 2019, at 8:23 PM, Guido van Rossum wrote:  On Fri, Sep 27, 2019 at 11:18 AM Serhiy Storchaka > wrote: I think it needs an explicit support in the type creation machinery (as __slots__ itself has)

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Eric V. Smith
> On Sep 27, 2019, at 8:23 PM, Guido van Rossum wrote: > >  >> On Fri, Sep 27, 2019 at 11:18 AM Serhiy Storchaka >> wrote: >> I think it needs an explicit support in the type creation machinery (as >> __slots__ itself has) to support descriptors and slots with the same name. > > That would

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Ricky Teachey
Here's an idea I was toying with in thinking about the problem this evening. Currently, python complains if you try to add a class member that will conflict with a slot: >>> class C: ... __slots__="x" ... x=1 ... Traceback (most recent call last): File "", line 1, in ValueError: 'x' in

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Andrew Barnert via Python-ideas
On Sep 27, 2019, at 17:20, Guido van Rossum wrote: > > Thinking aloud, perhaps this could be done by setting __slots__ to a magical > value, e.g. > > class Point: > __slots__ = "__auto__" > x: float > y: float Would this confuse any existing automated tools (or dataclass-like

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Gregory P. Smith
On Fri, Sep 27, 2019 at 5:21 PM Guido van Rossum wrote: > On Fri, Sep 27, 2019 at 11:18 AM Serhiy Storchaka > wrote: > >> I think it needs an explicit support in the type creation machinery (as >> __slots__ itself has) to support descriptors and slots with the same name. >> > > That would be

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Guido van Rossum
On Fri, Sep 27, 2019 at 11:18 AM Serhiy Storchaka wrote: > I think it needs an explicit support in the type creation machinery (as > __slots__ itself has) to support descriptors and slots with the same name. > That would be tough, since __slots__ is currently implemented by creating a separate

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Steven D'Aprano
On Fri, Sep 27, 2019 at 10:11:15AM -0700, Andrew Barnert wrote: > On Sep 27, 2019, at 09:05, Steven D'Aprano wrote: > > > > What > > I'd really like to do is use the collections.abc module to do the check: > > > >if isinstance(obj, collections.abc.Subscriptable): ... > > What about

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Eric V. Smith
On 9/27/2019 6:11 PM, Random832 wrote: On Fri, Sep 27, 2019, at 12:48, Andrew Barnert via Python-ideas wrote: or get rid of the guarantee that @dataclass returns your class with extra dunders. Why is dataclass a decorator instead of a metaclass (or, as below, pseudo-metaclass) anyway? Is it

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Steven D'Aprano
On Fri, Sep 27, 2019 at 12:03:52PM -0700, Andrew Barnert via Python-ideas wrote: > On Sep 27, 2019, at 10:15, MRAB wrote: > > > >> On 2019-09-27 17:05, Steven D'Aprano wrote: > >> > >> But doing it correctly is too painful: > >> if any(getattr(T, '__getitem__', None) is not None for T in >

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Ricky Teachey
Apologies, it was probably clear but I meant to say: > So if dataclass were a metaclass, it would not be possible to create a > dataclass using a second existing metaclass without multiple metaclass > inheritance... which, ain't nobody got time for THAT. > >

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Ricky Teachey
> > Why is dataclass a decorator instead of a metaclass (or, as below, > pseudo-metaclass) anyway? > One reason: because a class can only have one metaclass. So if dataclass were a metaclass, it would not be possible to create a dataclass using an existing metaclass multiple inheritance...

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Random832
On Fri, Sep 27, 2019, at 12:48, Andrew Barnert via Python-ideas wrote: > or get rid of the guarantee that @dataclass returns your > class with extra dunders. Why is dataclass a decorator instead of a metaclass (or, as below, pseudo-metaclass) anyway? Is it just that the decorator syntax looks

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Andrew Barnert via Python-ideas
On Sep 27, 2019, at 10:15, MRAB wrote: > >> On 2019-09-27 17:05, Steven D'Aprano wrote: >> >> But doing it correctly is too painful: >> if any(getattr(T, '__getitem__', None) is not None for T in >> type(obj).mro()) > [snip] > > Is there are reason why you're using 'getattr' instead of

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Serhiy Storchaka
27.09.19 19:48, Andrew Barnert via Python-ideas пише: On Friday, September 27, 2019, 07:47:41 AM PDT, Johnny Dahlberg wrote: > My proposal: Implement `@dataclass(slots=True)` which does the same thing as attrs: Replaces the class with a modified class that has a `__slots__` property

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Eric V. Smith
> On Sep 27, 2019, at 12:52 PM, Andrew Barnert via Python-ideas > wrote: > >  > On Friday, September 27, 2019, 07:47:41 AM PDT, Johnny Dahlberg > wrote: > > > My proposal: Implement `@dataclass(slots=True)` which does the same thing > > as attrs: Replaces the class with a modified class

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Andrew Barnert via Python-ideas
On Sep 27, 2019, at 09:05, Steven D'Aprano wrote: > > What > I'd really like to do is use the collections.abc module to do the check: > >if isinstance(obj, collections.abc.Subscriptable): ... What about isinstance(obj, (Sequence, Mapping))? That isn’t quite the same thing, since you can

[Python-ideas] Re: Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Andrew Barnert via Python-ideas
On Friday, September 27, 2019, 07:47:41 AM PDT, Johnny Dahlberg wrote: > My proposal: Implement `@dataclass(slots=True)` which does the same thing as > attrs: Replaces the class with a modified class that has a `__slots__` > property instead of a `__dict__`. And fully supporting default

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Devin Jeanpierre
On Fri, Sep 27, 2019 at 9:14 AM Steven D'Aprano wrote: > But doing it correctly is too painful: > > if any(getattr(T, '__getitem__', None) is not None for T in > type(obj).mro()) > For what it's worth, walking the MRO isn't necessary, and the None trick is only necessary if you want to

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Ivan Levkivskyi
Just FYI there is a closely related issue on b.p.o. https://bugs.python.org/issue25988. FWIW I am in favor of the idea, but some people are against it (see e.g. the issue). -- Ivan On Fri, 27 Sep 2019 at 17:12, Steven D'Aprano wrote: > I keep finding myself needing to test for objects that

[Python-ideas] Add Subscriptable ABC

2019-09-27 Thread Steven D'Aprano
I keep finding myself needing to test for objects that support subscripting. This is one case where EAFP is *not* actually easier: try: obj[0] except TypeError: subscriptable = False except (IndexError, KeyError): subscriptable = True else:

[Python-ideas] Add __slots__ to dataclasses to use less than half as much RAM

2019-09-27 Thread Johnny Dahlberg
It's pretty wasteful to use a dynamic storage dictionary to hold the data of a "struct-like data container". Users can currently add `__slots__` manually to your `@dataclass` class, but it means you can no longer use default values, and the manual typing gets very tedious. I compared the RAM

[Python-ideas] Re: Add a generic object comparison utility

2019-09-27 Thread Steven D'Aprano
On Fri, Sep 27, 2019 at 09:34:05AM +0700, Sébastien Eskenaz wrote: > I have a class `dataset` which can be built as follows: `mydata1 = > dataset('path/to/dataset1')` > Then I can create a second dataset `mydata2 = dataset('path/to/dataset1')` > (no typo about `path/to/dataset1`, it is a 1). >

[Python-ideas] Re: Add a generic object comparison utility

2019-09-27 Thread Sébastien Eskenaz
Ok, got the point. :-) Best regards, *Sébastien Eskenazi* Pixelz 15th Floor, Detech Tower 2 Building, 107 Nguyen Phong Sac, Hanoi, Vietnam Beautiful Images Sell Products Learn eCommerce

[Python-ideas] Re: Add a generic object comparison utility

2019-09-27 Thread Andrew Barnert via Python-ideas
On Sep 26, 2019, at 19:34, Sébastien Eskenaz wrote: > > Regarding parent class comparison, I would assume that there is a good reason > to have made that class `MyStr` hence either some of its methods or > attributes will end up being different from `str`. It may just override some of the