Python Forum
calling a function in a comprehension
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling a function in a comprehension
#1
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)]
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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)])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,453 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 833 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  Using list comprehension with 'yield' in function tester_V 5 1,264 Apr-02-2023, 06:31 PM
Last Post: tester_V
Sad Iterate randint() multiple times when calling a function Jake123 2 2,061 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,831 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  How to invoke a function with return statement in list comprehension? maiya 4 2,865 Jul-17-2021, 04:30 PM
Last Post: maiya
  [Solved] TypeError when calling function Laplace12 2 2,901 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  calling a function and argument in an input phillup7 3 2,628 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,255 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Calling DLL function OptoBruh 0 1,596 Nov-15-2019, 11:51 PM
Last Post: OptoBruh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020