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?
#3
This is all about contexts and closures.

I added some print statements to help track what is going on.
import time
def time_master(func):
    def call_func():
        print("start program")
        print("D", func)
        start = time.time()
        func()
        stop = time.time()
        print(f"it take {stop - start}second.")

    print("B", func)
    return call_func


def myfunc():
    time.sleep(2)
    print('I love fishc')

print("A", myfunc)
myfunc=time_master(myfunc)
print("C", myfunc)
myfunc()
Output:
A <function myfunc at 0x000001BE71C35360> B <function myfunc at 0x000001BE71C35360> C <function time_master.<locals>.call_func at 0x000001BE71C353F0> start program D <function myfunc at 0x000001BE71C35360> I love fishc it take 2.001746416091919second.
A: In the global context you define a function <function myfunc at 0x000001BE71C35360>. You assign this function to a variable named myfunc.

B: You pass this function as an argument to time_master. This creates a variable named func that exists inside the function time_master. Unlike most function variables, this one hangs around because we have a reference to the variable. Python creates a closure, kind of a persistent bubble of context that is used when we call myfunc() later on.

C: You assign the value returned by time_master <function time_master.<locals>.call_func at 0x000001BE71C353F0> to a variable named myfunc. myfunc now rferences time_master.call_func. You can no longer use myfunc to call the original function referenced by that variable.

D: You use the variable myfunc to call the time_master.call_func() function. This in turn calls the original myfunc function, not the function referenced by the myfunc variable.

Unlike variables in C++, myfunc is just a name that your program can use to reference python objects. It is not storage. It has no type. It is like the key in a dictionary. In this example it references a function , but I could write "myfunc = 42" and that would be a valid Python statement.
2367409125 likes this post
Reply


Messages In This Thread
RE: Why this code does not go into an infinite loop? - by deanhystad - Dec-02-2022, 08:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,240 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  Infinite loop problem Zirconyl 5 3,068 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  using 'while loop' output going into infinite loop... amitkb 2 2,019 Oct-05-2020, 09:18 PM
Last Post: micseydel
  Infinite loop not working pmp2 2 1,695 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Why is this code not an infinite loop? psi2kgcm 1 1,856 Dec-24-2019, 02:04 PM
Last Post: buran
  Appending to list not working and causing a infinite loop eiger23 8 4,070 Oct-10-2019, 03:41 PM
Last Post: eiger23
  Server infinite loop input from user tomislav91 1 4,272 May-23-2019, 02:18 PM
Last Post: heiner55
  Code Change Help: Infinite Loop Error bindulam 2 2,324 Mar-10-2019, 11:15 PM
Last Post: hshivaraj
  Question about for loop not creating an infinite loop. FWendeburg 1 2,149 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  Question about an infinite loop Robo_Pi 6 5,295 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