Python Forum
calling a function in a comprehension - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: calling a function in a comprehension (/thread-11530.html)



calling a function in a comprehension - Skaperen - Jul-13-2018

is there any chance foo() could be called more than once or is it well defined to be exactly once in this or any other comprehension construct?
    a=[bar(x) for x in foo(x)]



RE: calling a function in a comprehension - gontajones - Jul-13-2018

foo() will be called just at the beginning of the for loop.

def bar(n):
     return '0'+str(n)
def foo(n):
     return range(n)
print([bar(x) for x in foo(10)])