python - Strange behaviour Django shell and iPython -


i doing stuff in django console , realized global variables not recognized inside lambda expression, example if execute following code inside python or inside ipython console works perfectly:

a = 10 foo = lambda x: x + foo(10) # returns 20 

but if execute inside django shell ipython doesn't work:

in [8]: foo = lambda x: x +  in [9]: = 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 +  nameerror: global name 'a' not defined 

the ipython version 0.13.2

thank in advance!

edit

event if assign a before lambda funciton problem stills:

in [1]: = 10  in [2]: foo = lambda x: x +                                                                                                                                                                                                                    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 +  nameerror: global name 'a' not defined  in [4]:  ─────────── 

you might experiencing bug others have encountered here:

https://github.com/ipython/ipython/issues/62#issuecomment-3854940

as explained in thread little further down, prior version 1.6, django starting ipython shell using ipython.embed() function, forcing ipython start separate local , global namespaces.

the django team fixed issue in 1.6 in commit: https://github.com/django/django/commit/3570ff734e93f493e023b912c9a97101f605f7f5

here backport fix older versions of django (1.4.14 in case): https://github.com/theatlantic/django/commit/9dfe12c40af23956dc12e3427e3e7e63ebc360c9

you can reproduce issue if manually call ipython.embed() inside of function (creating closure), if use regular python/ipython shell. tested ipython 3.1.0:

>>> ipython import embed >>> def test(): ...     embed() ...  >>> test() python 2.7.9 (default, feb 10 2015, 03:28:08)  type "copyright", "credits" or "license" more information.  ipython 3.1.0 -- enhanced interactive python. ?         -> introduction , overview of ipython's features. %quickref -> quick reference.      -> python's own system. object?   -> details 'object', use 'object??' details.  in [1]: = 10  in [2]: foo = lambda x: x+a  in [3]: foo(10) --------------------------------------------------------------------------- nameerror                                 traceback (most recent call last) <ipython-input-3-08cbc4a9df91> in <module>() ----> 1 foo(10)  <ipython-input-2-3ecd5afea150> in <lambda>(x) ----> 1 foo = lambda x: x+a  nameerror: global name 'a' not defined 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -