Python Forum
Why this code does not go into an infinite loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why this code does not go into an infinite loop?
#1
Shocked 
import time
def time_master(func):
    def call_func():
        print("start program")
        start = time.time()
        func()
        stop = time.time()
        print(f"it take {stop - start}second.")

    return call_func


def myfunc():
    time.sleep(2)
    print('I love fishc')
myfunc=time_master(myfunc)
myfunc()
Hey, I am learning python, and I am confused with this code, this code create some functions to detect the times of runing other functions. However, in this case, at the end of the code, myfunc should equal to time_master(myfunc) right? so I believe when myfunc() recall the time_master function, in the time_master function -call_func() part, the myfunc function will be run again and start another time_master(myfunc) . Which will lead into an infinite loop, but it actually does not, can someone explain the reasons for me? thanks Rolleyes
Yoriz write Dec-02-2022, 05:41 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Why this code does not go into an infinite loop? - by 2367409125 - Dec-02-2022, 05:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,207 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  Infinite loop problem Zirconyl 5 3,044 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  using 'while loop' output going into infinite loop... amitkb 2 2,001 Oct-05-2020, 09:18 PM
Last Post: micseydel
  Infinite loop not working pmp2 2 1,679 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Why is this code not an infinite loop? psi2kgcm 1 1,848 Dec-24-2019, 02:04 PM
Last Post: buran
  Appending to list not working and causing a infinite loop eiger23 8 4,039 Oct-10-2019, 03:41 PM
Last Post: eiger23
  Server infinite loop input from user tomislav91 1 4,250 May-23-2019, 02:18 PM
Last Post: heiner55
  Code Change Help: Infinite Loop Error bindulam 2 2,289 Mar-10-2019, 11:15 PM
Last Post: hshivaraj
  Question about for loop not creating an infinite loop. FWendeburg 1 2,139 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  Question about an infinite loop Robo_Pi 6 5,256 Mar-05-2018, 05:46 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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