[Python-ideas] Syntax for easy binding __name__, __module__, __qualname__ to arbitrary objects

Martin Morrison mm at ensoft.co.uk
Mon May 13 02:44:15 CEST 2013


On 9 May 2013, at 11:29, Piotr Duda <duda.piotr at gmail.com> wrote:

> These also apply for other objects like NamedTuple or mentioned NamedValues.

+1
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. :-)

> To solve these problems I propose to add simple syntax that assigns
> these attributes to arbitrary object:
> def name = expression

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).

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 at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



More information about the Python-ideas mailing list