无法在python列表理解中调用iter链中的函数

时间:2013-05-16 19:20:22

标签: python list-comprehension itertools

我正在使用以下iter链来生成列表 -

from itertools import chain
from utils import my_func // custom function written that performs a function.
a = list(chain.from_iterable((u, my_func(u)) for u in MyCustomClass.objects.all()))

我希望会生成一个列表。 但是,我得到了错误 - NameError: global name 'my_func' is not defined

我做错了什么?

2 个答案:

答案 0 :(得分:1)

如果您正在使用Django / iPython,则Django存在一个错误:iPython是使用不同的命名空间启动的,这可以解释您的问题。

您可以尝试使用其他shell(基础python或bpython)或尝试patch您的Django安装。有关详细信息,请参阅此answer

答案 1 :(得分:0)

尝试utils.func(),可能是该函数超出当前范围