简体   繁体   English

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

[英]unable to call a function inside an iter chain in python list comprehension

I am using the following iter chain, to generate a list - 我正在使用以下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()))

I expect that a list will be generated. 我希望会生成一个列表。 But then, I get the error - NameError: global name 'my_func' is not defined 但是,然后我得到了错误NameError: global name 'my_func' is not defined

What am I doing wrong? 我究竟做错了什么?

If you are using Django/iPython there is a bug with Django: iPython is started with a different namespace, which could explain your problem. 如果您使用的是Django / iPython,则Django会出现一个错误:iPython是使用其他命名空间启动的,这可以解释您的问题。

You can try to use a different shell (base python or bpython) or try to patch your Django installation. 您可以尝试使用其他外壳(基本python或bpython)或尝试修补 Django安装。 See this answer for more information. 有关更多信息,请参见此答案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM