I'm not a typing expert, but I want to second Raymond's concerns, and perhaps
I'm qualified to do so as I gave the PyCon USA __slots__ talk this year and I
have a highly voted answer describing them on Stack Overflow.
Beautiful thing we're doing here with the dataclasses, by the way. I think
addressing the slots issue could be a killer feature of dataclasses.
I hope this doesn't muddy the water:
If I could change a couple of things about __slots__ it would be 1. to allow
multiple inheritance with multiple parents with nonempty slots (raises
"TypeError: multiple bases have instance lay-out conflict"), and
2. to avoid creating redundant slots if extant in a parent (but maybe we should
do this in the C level for all classes?).
It seems to me that Dataclasses could (and should) help us avoid the second
issue regardless (should be trivial to look in the bases for preexisting slots,
right?).
My workaround for the first issue is to inherit from ABCs with empty slots, but
you need cooperative multiple inheritance for this - and you need to track the
expected attributes (easy if you use abstract properties, which slots provide
for. Maybe not all users of Dataclasses are advanced enough to do this?
So, maybe this is crazy (please don't call the nice men in white coats on me),
came to me as I was responding, and definitely outside the box here, but
perhaps we could make decorated dataclass be the abstract parent of the
instantiated class?
Thanks,
Aaron Hall
On Friday, December 8, 2017, 1:31:44 PM EST, Raymond Hettinger
<[email protected]> wrote:
The way __slots__ works is that the type() metaclass automatically assigns
member-objects to the class variables 'x' and 'y'. Member objects are
descriptors that do the actual lookup.
So, I don't think the language limitation can be "fixed". Essentially, we're
wanting to use the class variables 'x' and 'y' to hold both member objects and
a default value.
I recommend that you follow the path taken by attrs and return a new class.
Otherwise, we're leaving users with a devil's choice. You can have default
values or you can have slots, but you can't have both.
The slots are pretty important. With slots, a three attribute instance is only
64 bytes. Without slots, it is 296 bytes.
I'm hoping the typing experts will chime in here. The question is
straight-forward. Where should we look for the signature and docstring for
constructing instances? Should they be attached to the class, to __init__(),
or to __new__() when it used.
It would be nice to have an official position on that before, it gets set in
stone through arbitrary choices made by pycharm, pydoc, mypy,
typing.NamedTuple, and dataclasses.dataclass.
Raymond
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/aaronchall%40yahoo.com
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com