I was doing some stuff in the Django console and I realized that the global variables are not recognized inside the lambda
expression, for example if you execute the following code inside a python or even inside an iPython console it works perfectly:
a = 10
foo = lambda x: x + a
foo(10) # returns 20
But if you execute it inside the Django shell with iPython it doesn't work:
In [8]: foo = lambda x: x + a
In [9]: a = 10
In [10]: foo(10)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 foo(10)
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <lambda>(x)
----> 1 foo = lambda x: x + a
NameError: global name 'a' is not defined
The iPython version 0.13.2
Thank you in advance!
EDIT
Event if I assign a
before the lambda funciton the problem stills:
In [1]: a = 10
In [2]: foo = lambda x: x + a
In [3]: foo(10)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 foo(10)
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <lambda>(x)
----> 1 foo = lambda x: x + a
NameError: global name 'a' is not defined
In [4]:
───────────