简体   繁体   中英

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

I am using the following iter chain, to generate a list -

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

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.

You can try to use a different shell (base python or bpython) or try to patch your Django installation. See this answer for more information.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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