[Python-ideas] Assignments in list/generator expressions
Arnaud Delobelle
arnodel at gmail.com
Sun Apr 10 12:37:42 CEST 2011
On 10 Apr 2011, at 08:41, Nick Coghlan wrote:
> On Sun, Apr 10, 2011 at 2:27 AM, Eugene Toder <eltoder at gmail.com> wrote:
>> I'd rather add syntax equivalent to Haskell's let, than inventing
>> completely new syntax. E.g. with name = expr. Original from the first
>> post becomes
>>
>> ys = [y for x in xs with y = f(x) if y]
>>
>> but it's more flexible in general.
>
> It isn't strangely missing at all, it's just hard:
> http://www.python.org/dev/peps/pep-3150/
I have just read the proposal and I would like to suggest an idea for implementing it. It may not be a good idea but at least it's simple! Given the "torture test", at the end of the proposal:
b = {}
a = b[f(a)] = x given:
x = 42
def f(x):
return x
assert "x" not in locals()
assert "f" not in locals()
assert a == 42
assert d[42] == 42 given:
d = b
assert "d" not in locals()b = {}
Without "given", one would write it as below. Obviously it succeeds at module, local and class scope.
b = {}
x = 42
def f(x):
return x
a = b[f(a)] = x
del x
del f
assert "x" not in locals()
assert "f" not in locals()
assert a == 42
d = b
assert d[42] == 42
del d
assert "d" not in locals()
The only (but big!) problem is that this deletes any previously defined variable "x", "f", and "d" in the current scope. So why not just rename the variables "x", "f", and "d" at compile time to something that is guaranteed not to appear elsewhere in the scope, e.g. "@1", "@2", "@3" or some other naming scheme, in the style of the old "gensym" function in Lisp?
So we get:
b = {}
@1 = 42
def @2(x):
return x
a = b[@2(a)] = @1
del @1
del @2
assert "x" not in locals()
assert "f" not in locals()
assert a == 42
@3 = b
assert @3[42] == 42
del @3
assert "d" not in locals()
--
Arnaud
More information about the Python-ideas
mailing list