[Python-ideas] Quick idea: defining variables from functions that take the variable name

Steven D'Aprano steve at pearwood.info
Wed Jun 1 11:36:40 EDT 2016


On Wed, Jun 01, 2016 at 11:24:35AM -0400, marky1991 . wrote:
> What is the value of __assigned_name__ here:
> 
> x = y = z = AutoSymbol()
> 
> ? Is it a list?

With my proposal, that would be a syntax error. However this would be 
allowed:


x, y, z -> Symbol()  # there's nothing "auto" about it


and Symbol would receive a single argument, a tuple ('x', 'y', 'z'). 
Presumably then it could:

* raise an exception, to indicate that it doesn't support creating 
  three symbols at once;

* return three different symbols, one for each of the names;

* return a single symbol duplicated three times

whatever makes sense for the application.

But the important thing is, this would be *exactly* the same as:

x, y, z = Symbol(('x', 'y', 'z'))

The only magic is that you don't have to manually copy the names from 
the left hand side over onto the right and quote them. All Symbol knows 
is that it got passed a three-tuple as argument. It cannot know how it 
got there.


-- 
Steve


More information about the Python-ideas mailing list