user1629366
user1629366

Reputation: 2011

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?

Upvotes: 0

Views: 110

Answers (2)

Salem
Salem

Reputation: 12986

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.

Upvotes: 1

Pieterjan
Pieterjan

Reputation: 475

Try utils.func(), could be that the function is out of the current scope

Upvotes: 0

Related Questions