Python Forum
Function Recognises Variable Without Arguments Or Global Variable Calling.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function Recognises Variable Without Arguments Or Global Variable Calling.
#2
(Apr-06-2020, 08:44 AM)OJGeorge4 Wrote: 1) does python search for global value (and use them) by default if local not found? (or does it only do this if the input args are empty?

The first. It will use a variable from a larger scope (such as global) if there isn't a local binding variable. The follow-on is that there is no local binding variable if it is not assigned (written to) within the function.

Compare:

a = 10

def function():
    print(a)

function()
and

a = 10

def function():
    print(a)
    a = 1

function()
Because a is assigned at some point in the second function, it is a local variable.

Quote:2) why if you use a while loop like below, do I get the error of not finding the global variable, but don't get that error for finding a,b etc.?

Because a and b were never assigned in the function, but is_true is assigned within.

From Python Docs
Quote:If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations.

The global and nonlocal statements can override this behavior.
Reply


Messages In This Thread
RE: Function Recognises Variable Without Arguments Or Global Variable Calling. - by bowlofred - Apr-06-2020, 09:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 355 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Commas issue in variable ddahlman 6 529 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 290 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 241 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 368 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  optimum chess endgame with D=3 pieces doesn't give an exact moves_to_mate variable max22 1 309 Mar-21-2024, 09:31 PM
Last Post: max22
  unbounded variable akbarza 3 548 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 726 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 484 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  working directory if using windows path-variable chitarup 2 775 Nov-28-2023, 11:36 PM
Last Post: chitarup

Forum Jump:

User Panel Messages

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