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 descriptor for each slot.
One thought I've had about __slots__ would be it'd be nice to take a
dictionary in the form of:
class C:
__slots__ = {'a': ???, 'b': ???}
You could actually provide this dictionary today, but the values would be
ignored. The values could start to do interesting things. One flavor of
that would be that they could indicate the underlying storage used for the
slots (maybe with 'i' for int32, 'b' for byte, 'l' for long, or whatever
color encoding sounds good). This is just mapping into the available
storage types that are already available in structmember.c. That's just
extending the existing use case of slots as being a more memory efficient
storage representation, and might help people avoid dropping into Cython
just to get compact instance members.
But another application of that could be accepting a callable which would
then receive the descriptor, and return a new descriptor. One example of
what that'd let you do is build a cached-property decorator that would do
the get/sets into the slot. But presumably it would also provide a way for
other scenarios where you want to explicitly collide with the get/set
descriptor with a member. It doesn't help so much with the verbosity of
defining these things that I think was mentioned elsewhere in this thread.
And it doesn't play so well w/ class decorators, but could be more usable
with meta-classes.
On Fri, Sep 27, 2019 at 5:21 PM 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 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
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. :-)
-- --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-c... _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/3632QS... Code of Conduct: http://python.org/psf/codeofconduct/