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.
#1
Hi all,

I am struggling to understand why this code runs correctly and I can't seem to find an answer online.

a = 10
b = 5

def function():
    new_local_variable = a
    other_local_variable = new_local_variable * b
    print(other_local_variable)

function()
In the video I watched it runs correctly (prints 'other local variable') and uses the global value of 'a' inside the function without input arguments in the function definition OR calling the global variable 'a' into the function.

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?

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.?
ERROR: "UnboundLocalError: local variable 'is_true' referenced before assignment" :


a = 10
b = 5
is_true = True

def function():
    iter = 0
    while is_true:
        new_local_variable = a
        other_local_variable = new_local_variable * b
        print(other_local_variable)
        
        iter += 1
        
        if iter == 20:
            is_true = False
function()
Am I being really dumb?

Thanks so much in advance.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions: why use a variable "display" in a UDF as a Boolean MMartin71 3 321 May-26-2025, 05:55 AM
Last Post: DeaD_EyE
  PYTHONHOME Variable correct setting msetzerii 0 181 May-25-2025, 11:48 PM
Last Post: msetzerii
  function arguments Curbie 2 574 Apr-30-2025, 05:23 PM
Last Post: Curbie
  how to get variable Azdaghost 3 606 Apr-23-2025, 07:43 PM
Last Post: deanhystad
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 378 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 740 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  creating arbitrary local variable names Skaperen 9 2,107 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  Variable Substitution call keys Bobbee 15 3,204 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  how solve: local variable referenced before assignment ? trix 5 1,954 Jun-15-2024, 07:15 PM
Last Post: trix
  Variable being erased inside of if statement deusablutum 8 2,408 Jun-15-2024, 07:00 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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