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

Raymond Hettinger raymond.hettinger at gmail.com
Mon May 13 03:33:47 CEST 2013


On May 9, 2013, at 3:29 AM, Piotr Duda <duda.piotr at gmail.com> wrote:
> 
> Animals = Enum('Animals', 'dog cat bird')
> which violates DRY

This is a profound misreading of DRY which is all about not repeating
big chunks of algorithmic logic.

The above code is clear and doesn't require any special syntax.
Remember, that is why we got rid of the print-keyword in favor
of the print-function (the new print requires no special rules
and works like ordinary functions).

Also remember that Enum and NamedTuple calls typically only
occur once in code.  The bulk of the code simply *uses* the 
declared enumerations or named tuples.

In other words, you're inventing new syntax to solve a very
unimportant problem.  In most code, you will save perhaps
one single word, but it will come at the expense of an even
and odd and unexpected 'def' syntactic magic:

old:  Animals = Enum('Animals', 'dog cat bird')  
new:  def Animals  = Enum('dog cat bird')
net savings:  6 characters
net loss:  complexify Enum, abuse the def syntax, looks weird, ...

old:  a = Animals.dog; b=Animals.cat
new:  a = Animals.dog; b=Animals.cat
new change where it matters:  zero!


> These also apply for other objects like NamedTuple or mentioned NamedValues.
> 
> 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
> 
> <snip>
> 
> 
> Other examples:
> def MyTuple = NamedTuple("a b c d")
> def PI = NamedValue(3.1415926)

Sorry, but I think this is yet another terrible idea.
People like Python because of its beautiful and
intuitive syntax.  Why throw that out the window
for such an unimportant problem?


Raymond











More information about the Python-ideas mailing list