Django shell和iPython的奇怪行为
我在Django控制台中做了一些工作,我意识到在Django shell和iPython的奇怪行为,python,django,python-2.7,ipython,django-shell,Python,Django,Python 2.7,Ipython,Django Shell,我在Django控制台中做了一些工作,我意识到在lambda表达式中无法识别全局变量,例如,如果您在python中执行以下代码,甚至在iPython控制台中执行以下代码,那么它就可以完美地工作: a = 10 foo = lambda x: x + a foo(10) # returns 20 但是,如果在Django shell中使用iPython执行它,它将无法工作: In [8]: foo = lambda x: x + a In [9]: a = 10 In [10]: foo(1
lambda
表达式中无法识别全局变量,例如,如果您在python中执行以下代码,甚至在iPython控制台中执行以下代码,那么它就可以完美地工作:
a = 10
foo = lambda x: x + a
foo(10) # returns 20
但是,如果在Django shell中使用iPython执行它,它将无法工作:
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
您可能遇到了其他人在此处遇到的错误: 在1.6版之前的线程中,django使用
ipython.embed()
函数启动ipython shell,这迫使ipython使用单独的本地和全局名称空间启动
django团队在1.6中修复了此问题:
以下是Django旧版本(本例中为1.4.14)的后端口修复程序:
如果在另一个函数(创建闭包)内部手动调用IPython.embed()
,即使使用常规python/IPython shell,也可以重现此问题。使用ipython 3.1.0进行测试:
>>> from IPython import embed
>>> def test():
... embed()
...
>>> test()
Python 2.7.9 (default, Feb 10 2015, 03:28:08)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: a = 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' is not defined
>>来自IPython导入嵌入
>>>def test():
... 嵌入()
...
>>>测试()
Python 2.7.9(默认值,2015年2月10日,03:28:08)
有关详细信息,请键入“版权”、“信用”或“许可证”。
iPython3.1.0——一种增强的交互式Python。
? -> 介绍和概述IPython的功能。
%快速参考->快速参考。
帮助->Python自己的帮助系统。
对象?->有关“对象”的详细信息,请使用“对象??”获取更多详细信息。
[1]中:a=10
[2]中:foo=lambda x:x+a
在[3]中:foo(10)
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在()
---->1富(10)
in(x)
---->1 foo=λx:x+a
NameError:未定义全局名称“a”
您可能遇到了其他人在此处遇到的错误:
在1.6版之前的线程中,django使用ipython.embed()
函数启动ipython shell,这迫使ipython使用单独的本地和全局名称空间启动
django团队在1.6中修复了此问题:
以下是Django旧版本(本例中为1.4.14)的后端口修复程序:
如果在另一个函数(创建闭包)内部手动调用IPython.embed()
,即使使用常规python/IPython shell,也可以重现此问题。使用ipython 3.1.0进行测试:
>>> from IPython import embed
>>> def test():
... embed()
...
>>> test()
Python 2.7.9 (default, Feb 10 2015, 03:28:08)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: a = 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' is not defined
>>来自IPython导入嵌入
>>>def test():
... 嵌入()
...
>>>测试()
Python 2.7.9(默认值,2015年2月10日,03:28:08)
有关详细信息,请键入“版权”、“信用”或“许可证”。
iPython3.1.0——一种增强的交互式Python。
? -> 介绍和概述IPython的功能。
%快速参考->快速参考。
帮助->Python自己的帮助系统。
对象?->有关“对象”的详细信息,请使用“对象??”获取更多详细信息。
[1]中:a=10
[2]中:foo=lambda x:x+a
在[3]中:foo(10)
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在()
---->1富(10)
in(x)
---->1 foo=λx:x+a
NameError:未定义全局名称“a”
您可能遇到了其他人在此处遇到的错误:
在1.6版之前的线程中,django使用ipython.embed()
函数启动ipython shell,这迫使ipython使用单独的本地和全局名称空间启动
django团队在1.6中修复了此问题:
以下是Django旧版本(本例中为1.4.14)的后端口修复程序:
如果在另一个函数(创建闭包)内部手动调用IPython.embed()
,即使使用常规python/IPython shell,也可以重现此问题。使用ipython 3.1.0进行测试:
>>> from IPython import embed
>>> def test():
... embed()
...
>>> test()
Python 2.7.9 (default, Feb 10 2015, 03:28:08)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: a = 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' is not defined
>>来自IPython导入嵌入
>>>def test():
... 嵌入()
...
>>>测试()
Python 2.7.9(默认值,2015年2月10日,03:28:08)
有关详细信息,请键入“版权”、“信用”或“许可证”。
iPython3.1.0——一种增强的交互式Python。
? -> 介绍和概述IPython的功能。
%快速参考->快速参考。
帮助->Python自己的帮助系统。
对象?->有关“对象”的详细信息,请使用“对象??”获取更多详细信息。
[1]中:a=10
[2]中:foo=lambda x:x+a
在[3]中:foo(10)
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在()
---->1富(10)
in(x)
---->1 foo=λx:x+a
NameError:未定义全局名称“a”
您可能遇到了其他人在此处遇到的错误:
在1.6版之前的线程中,django使用ipython.embed()
函数启动ipython shell,这迫使ipython使用单独的本地和全局名称空间启动
django团队在1.6中修复了此问题:
以下是Django旧版本(本例中为1.4.14)的后端口修复程序:
如果在另一个函数(创建闭包)内部手动调用IPython.embed()
,即使使用常规python/IPython shell,也可以重现此问题。使用ipython 3.1.0进行测试:
>>> from IPython import embed
>>> def test():
... embed()
...
>>> test()
Python 2.7.9 (default, Feb 10 2015, 03:28:08)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: a = 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' is not defined
>>来自IPython导入嵌入
>>>def test():
... 嵌入()
...
>>>测试()
Python 2.7.9(默认值,2015年2月10日,03:28:08)
有关详细信息,请键入“版权”、“信用”或“许可证”。
iPython3.1.0——一种增强的交互式Python。
? -> 介绍和概述IPython的功能。
%快速参考->快速参考。
帮助->Python自己的帮助系统。
对象?->有关“对象”的详细信息,请使用“对象??”获取更多详细信息。
[1]中:a=10
[2]中:foo=lambda x:x+a
在[3]中:foo(10)
---------------------------------------------------------------------------
纳梅尔