2

I am kind of a beginner with Python and have dealt with the concept of a global variable. When I thought I understood the concept, I saw a short code which proved me wrong:

message="global"

def enclosure():

     message="enclosure"

     def local():
         global message
         message="local"

     local()
     print(message)

print(message)
enclosure()
print(message)

The output of that is:

global 
enclosure
local

I dont understand, why the second output is enclosure, because when you call the enclosure function, within this function the funciton local is called, in which the global variable message is set to "local", which then as of my understanding should be printed out at the end when calling the enclosure function (so i would expect global, local, local..).

So where is my thinking error?

0

4 Answers 4

1

When you assigned message="enclosure" in enclosure, you made message a local variable for that scope. It doesn't matter that the nested function changed the global message; enclosure didn't declare message global, so it maintains its own locally scoped variable independent of the global one (that local shares).

2
  • I just read about declaring it as nonlocal, that would help in that case, right?
    – Mh Aztec
    Mar 8, 2018 at 11:25
  • @MhAztec: It would only matter inside the local method, where it would cause the assignment to apply to enclosure's method, not the global method. Used inside enclosure, it would be equivalent to global. Mar 8, 2018 at 11:35
0

In general, you should try to avoid global variables if possible. There is usually a cleaner way to achieve what you want.

In your example, you have two string variables, both called message. One has the scope of the whole module, it is initially bound to the value "global" and then inside the local method it is rebound to the value "local".

The second variable is also called message and has the scope of the enclosure method. It is initially set to the value "enclosure" and is not modified.

Your first print call is at module scope, so prints the value of the module-scoped message, which is "global". Your second call is inside the enclosure so prints the value of the other variable which is "enclosure". And finally you print the value of the module-scoped message which has now been changed to the value "local".

0

In reality you have two variables, for clarity I added a suffix 1 and 2:

message1="global"

def enclosure():
    message2="enclosure"   # this is a new declaration, it is a new message.

    def local():
       global message1
       message1="local"

   local()
   print(message2)   # in this scope, only message2 is visible

print(message1)
enclosure()
print(message1)

So you see, you created two message, and you use at the second print the second variable named message

0

i hope this will help you:

def generate_closure(income_tax_rate = 0.33):



    def this_is_closure_which_will_give_final_money(raw_income):


        final_money = raw_income - (raw_income * income_tax_rate)
        return final_money

    return this_is_closure_which_will_give_final_money


standard_tax_calculator = generate_closure()

classic_salary = standard_tax_calculator(2000)
final_classic_money = print(classic_salary)

classic_salary_2 = standard_tax_calculator(2499)
final_classic_money_2 = print(classic_salary_2)


rich_people_tax_calculator = generate_closure(income_tax_rate = 0.50)
rich_salary = rich_people_tax_calculator(15000)
final_rich_money = print(rich_salary)

rich_salary_2 = rich_people_tax_calculator(19499)
final_rich_money_2 = print(rich_salary_2)


print("\n------------------------------")


def generate_closure(trapped):
    def closure(x):
        return trapped ** x
    return closure


f = generate_closure(10)
print(f)

f(3)

print(f(3)) # f remember the value 10

del generate_closure

print(f(2)) # it still does work !

print(f.__closure__[0].cell_contents) # here is the hidden value

Here is a function which return another function. A closure is not a variable.

A closure aims at avoid using global variable.

Take care not to use key-word the wrong way.

You can think to use closure when some argument it will use are known to be constant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.