On 9 May 2013, at 11:29, Piotr Duda <duda.piotr@gmail.com> wrote:+1
> These also apply for other objects like NamedTuple or mentioned NamedValues.
I really like this proposal.
In one of my libraries, I actually went one step further with the frame hack and injected the resulting object into the parent namespace in an attempt to avoid the duplicated declaration of the name. The syntax was something like:
namedtuple.MyName("foo", "bar")
as a bare statement, which would inject "MyName" into the namespace (using the frame hack) as well as getting the right module name. This is obviously very, very ugly. :-)
FWIW, this syntax looks the most obvious to me, as it clearly communicates both the assignment of the return value of the expression to the name, and the fact that name is a local definition (and thus likely to acquire additional properties).
> To solve these problems I propose to add simple syntax that assigns
> these attributes to arbitrary object:
> def name = expression
Cheers,
Martin
> 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
>
> with new syntax declaring Enum will look like
> def Animals = Enum('dog cat bird')
>
> as pointed by Larry it may be done using existing syntax in form:
> @Enum('dog cat bird')
> def Animals(): pass
>
> but it's ugly, and may by confusing.
>
>
> Other examples:
> def MyTuple = NamedTuple("a b c d")
> def PI = NamedValue(3.1415926)
>
>
> --
> 闇に隠れた黒い力
> 弱い心を操る
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> http://mail.python.org/mailman/listinfo/python-ideas
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
http://mail.python.org/mailman/listinfo/python-ideas