1

Here I wrote two simple programs and expected the results to be same: program 1(result=123):

price=[]
def checkCondition(a,b):
    if a<b:
        price.append(123)
if  __name__ == '__main__':
    checkCondition(1,2)
print price[0]

program 2(result=''):

price=''
def checkCondition(a,b):
    if a<b:
        price=123
if  __name__ == '__main__':
    checkCondition(1,2)
print price

Why it couldn't assign 123 to price in program 2?

kevinL
  • 57
  • 6

4 Answers4

2

You're assigning a local variable. Tell Python to use the global variable instead:

price=''
def checkCondition(a,b):
    global price # now reassigning price
    if a<b:
        price=123
if  __name__ == '__main__':
    checkCondition(1,2)
print price

From the Python doc Naming and binding:

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. [...]

If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace.

tynn
  • 38,113
  • 8
  • 108
  • 143
0

There are two types of python variables: globals and locals. Globals are reachable from all positions in your code. But locals are only reachable from the def() - definition they were created from. By default variables you create in a function are local and so there is a global price (='') amd a local price in your function(=123). To make it global and replace the existing global variable with the new one type on the beginning of your function definition:

global price

This makes the price variable in your functuon global.

The first one worked because your were not making a local variable. You did something on an existing variable (price). If the existing variable is global or not is not important then.

Faminator
  • 888
  • 1
  • 10
  • 16
0

You need to tell the interpreter that you are referring to global name, as names inside functions are local to their scope by default:

price=''
def checkCondition(a,b):
    global price
    if a<b:
        price=123
if  __name__ == '__main__':
    checkCondition(1,2)
print price
Zah
  • 6,394
  • 1
  • 22
  • 34
-1

Note that inside a function, you can use a global variable, but can't change it without declaring it with global (see here).

The quote in the doc that matters:

It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.

The first program used the variable by calling a method, which is okay and it price resolves to the global variable. For the second you intended to change a global variable's value, which isn't doable unless the variable is declared with global. Since you can't change the global variable's value, it will create a new local variable and assign the value to it.

To fix it, you need to use global to declare it first:

price=[]
def checkCondition(a,b):
    global price
    if a<b:
        price.append(123) # also works without the global
        price = 123
if  __name__ == '__main__':
    checkCondition(1,2)
print price
zw324
  • 26,764
  • 16
  • 85
  • 118