On Fri, Sep 27, 2019 at 7:12 PM Eric V. Smith <e...@trueblade.com> wrote:

>
> On Sep 27, 2019, at 8:23 PM, Guido van Rossum <gu...@python.org> wrote:
>
> 
> On Fri, Sep 27, 2019 at 11:18 AM Serhiy Storchaka <storch...@gmail.com>
> 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 descriptor for each slot.
>
> I do think that it would be nice to have a way to automatically create
> __slots__ from annotations. It would be even nicer if that could be done
> without copying the class object (as the current state of the art requires:
> https://github.com/ericvsmith/dataclasses/blob/master/dataclass_tools.py#L23
> ).
>
> Thinking aloud, perhaps this could be done by setting __slots__ to a
> magical value, e.g.
>
> class Point:
>     __slots__ = "__auto__"
>     x: float
>     y: float
>
> I think a sentinel like None or a new  typing.use_annotations_for_slots
> (need a better name, of course) would be better than a magic string,
> especially since strings are iterable.
>

 If `__slots__` is a string, it will be considered the name of the single
slot to be created. But we have the namespace of __dunder__ names reserved,
so `__slots__ = "__auto__"` should be okay. (But `__slots__ = "auto"` would
not be.)

I'm not keen on `None`, that looks like it would mean "no slots" (analogous
to `__hash__ = None`).

> This would be independent from the @dataclass decorator (though the
> decorator may have to be aware of the magic value).
>
> If that's too wacky, we could also use a class keyword argument:
>
> class Point(slots=True):
>     x: float
>     y: float
>
> (Though arguably that's just as wacky. :-)
>
>
> I like a special value for __slots__. I might look in to the feasibility
> of this.
>

Please do. It sounds simpler than the `(slots=True)` version -- IIRC
keyword args to classes are hard to work with.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2H3ZF3XPXV645275CEIVUKHPLH3JLV6W/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to