[Python-ideas] Syntax for easy binding __name__, __module__, __qualname__ to arbitrary objects
Paul Moore
p.f.moore at gmail.com
Thu May 9 13:19:10 CEST 2013
On 9 May 2013 11:29, Piotr Duda <duda.piotr at gmail.com> wrote:
>
> To solve these problems I propose to add simple syntax that assigns
> these attributes to arbitrary object:
> def name = expression
> other possible forms may be:
> def name from expression
> class name = expression
> class name from expression
> name := expression # new operator
>
>
> which would be equivalent for:
> _tmp = expression
> _tmp.__name__ = 'name'
> _tmp.__qualname__ = ... # corresponding qualname
> _tmp.__module__ = __name__
> # apply decorators if present
> name = _tmp
>
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,
def x = 12
This isn't a point against the syntax, I just think it's worth being
explicit that this is what would happen.
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.
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).
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130509/a5dc21ff/attachment.html>
More information about the Python-ideas
mailing list