<div dir="ltr"><div class="gmail_extra">On 9 May 2013 11:29, Piotr Duda <span dir="ltr"><<a href="mailto:duda.piotr@gmail.com" target="_blank">duda.piotr@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
To solve these problems I propose to add simple syntax that assigns<br>
these attributes to arbitrary object:<br>
def name = expression<br>
other possible forms may be:<br>
def name from expression<br>
class name = expression<br>
class name from expression<br>
name := expression # new operator<br>
<br>
<br>
which would be equivalent for:<br>
_tmp = expression<br>
_tmp.__name__ = 'name'<br>
_tmp.__qualname__ = ... # corresponding qualname<br>
_tmp.__module__ = __name__<br>
# apply decorators if present<br>
name = _tmp<br></blockquote><div> </div><div>Just for clarification, if you used this syntax with an expression which returned an object which *didn't* allow attributes to be set, I assume it would simply fail at runtime with an AttributeError? For example,<div>
<br></div><div>def x = 12</div><div><br></div></div><div style>This isn't a point against the syntax, I just think it's worth being explicit that this is what would happen.</div><div style><br></div><div style>Overall, I'm somewhat indifferent. The use case seems fairly specialised to me, and yet the syntax "def name = value" seems like it's worth reserving for something a bit more generally useful.</div>
<div style><br></div><div style>Maybe the def name=value syntax should implement a protocol, that objects like enum and namedtuple subclasses can hook into (in the same way that the context manager and iterator protocols work, or indeed the whole class definition mechanism).</div>
<div style><br></div><div style>Paul</div></div></div></div>