<div dir="ltr"><div>Yes, I think this is a reasonable argument for adding a 'slots' option (off by default) for @dataclass(). However I don't think we need to rush it in. I'm not very happy with the general idea of slots any more, and I think that it's probably being overused, and at the same time I expect that there are a lot of classes with a slots declaration that still have a dict as well, because they inherit from a class without slots.<br><br></div>I'm not sure what to do about docstrings -- I'm not a big user of pydoc and I find help() often too verbose (I usually read the source. Maybe we could add a 'doc' option to field()? That's similar to what we offer for property(). </div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 7, 2017 at 12:47 PM, Eric V. Smith <span dir="ltr"><<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 12/7/17 3:27 PM, Raymond Hettinger wrote:<br>
...<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm looking for guidance or workarounds for two issues that have arisen.<br>
<br>
First, the use of default values seems to completely preclude the use of __slots__. For example, this raises a ValueError:<br>
<br>
  class A:<br>
    __slots__ = ['x', 'y']<br>
    x: int = 10<br>
    y: int = 20<br>
</blockquote>
<br></span>
Hmm, I wasn't aware of that. I'm not sure I understand why that's an error. Maybe it could be fixed?<br>
<br>
Otherwise, I have a decorator that takes a dataclass and returns a new class with slots set:<br>
<br>
>>> from dataclasses import dataclass<br>
>>> from dataclass_tools import add_slots<br>
>>> @add_slots<br>
... @dataclass<br>
... class C:<br>
...  x: int = 0<br>
...  y: int = 0<br>
...<br>
>>> c = C()<br>
>>> c<br>
C(x=0, y=0)<br>
>>> c.z = 3<br>
Traceback (most recent call last):<br>
 File "<stdin>", line 1, in <module><br>
AttributeError: 'C' object has no attribute 'z'<br>
<br>
This doesn't help the general case (your class A), but it does at least solve it for dataclasses. Whether it should be actually included, and what the interface would look like, can be (and I'm sure will be!) argued.<br>
<br>
The reason I didn't include it (as @dataclass(slots=True)) is because it has to return a new class, and the rest of the dataclass features just modifies the given class in place. I wanted to maintain that conceptual simplicity. But this might be a reason to abandon that. For what it's worth, attrs does have an @attr.s(slots=True) that returns a new class with __slots__ set.<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The second issue is that the different annotations give different signatures than would produced for manually written classes. It is unclear what the best practice is for where to put the annotations and their associated docstrings.<br>
</blockquote>
<br></span>
I don't have any suggestions here.<span class="HOEnZb"><font color="#888888"><br>
<br>
Eric.</font></span><div class="HOEnZb"><div class="h5"><br>
______________________________<wbr>_________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/options/python-dev/guido%<wbr>40python.org</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>