Python Forum
Which GUI can have indefinite loop ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Which GUI can have indefinite loop ?
#13
This works as YOU would expect.
a = 42

def func():
    return a

print(func())
Output:
42
Because a is not assigned a value in func(), the function does not create a local "a" variable and the "a" used in func() is the a from the global scope (a = 42)

This is going to surprise you.
a = 42

def func():
    a = "Hi Mom"
    print("a inside func =", a)

func()
print("a outside func =", a)
a inside func = Hi Mom
a outside func = 42
Here we have two variables named "a"; on in the global scope (a = 42) and one in the function scope (a = "Hi Mom"). func() does not use the global "a" because func() assigns a value to "a" it creates its own "a" variable.

And for completeness:
a = 42

def func():
    global a
    a = "Hi Mom"
    print("a inside func =", a)

func()
print("a outside func =", a)
Output:
a inside func = Hi Mom a outside func = Hi Mom
In this example there is only one variable named "a". The "global a" inside func() tells python to use "a" from the global scope instead of creating a local variable.
jst likes this post
Reply


Messages In This Thread
Which GUI can have indefinite loop ? - by jst - Nov-15-2023, 06:53 PM
RE: Which GUI can have indefinite loop ? - by jst - Nov-15-2023, 07:10 PM
RE: Which GUI can have indefinite loop ? - by jst - Nov-15-2023, 07:42 PM
RE: Which GUI can have indefinite loop ? - by jst - Nov-15-2023, 09:20 PM
RE: Which GUI can have indefinite loop ? - by jst - Nov-15-2023, 09:23 PM
RE: Which GUI can have indefinite loop ? - by jst - Nov-15-2023, 09:37 PM
RE: Which GUI can have indefinite loop ? - by jst - Nov-16-2023, 05:28 AM
RE: Which GUI can have indefinite loop ? - by deanhystad - Nov-16-2023, 06:16 AM
RE: Which GUI can have indefinite loop ? - by jst - Nov-16-2023, 07:32 AM
RE: Which GUI can have indefinite loop ? - by jst - Nov-16-2023, 08:41 AM
RE: Which GUI can have indefinite loop ? - by jst - Nov-16-2023, 09:35 AM
RE: Which GUI can have indefinite loop ? - by jst - Nov-16-2023, 08:01 PM
RE: Which GUI can have indefinite loop ? - by jst - Nov-16-2023, 10:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Init an indefinite number of class MathisDELAGE 9 2,415 Feb-18-2022, 07:49 PM
Last Post: deanhystad
  Indefinite loop ( I think ) marsh20 2 1,984 Aug-20-2020, 12:33 PM
Last Post: deanhystad
  indefinite loop Johnygo 3 2,176 Jul-03-2019, 12:53 PM
Last Post: Johnygo

Forum Jump:

User Panel Messages

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