-3

let's say we have a construct like this:

x = ""
def xy():
    x = "String"

Now when calling xy() and printing x afterwards, it's empty.
Trying to declare the variable as global, like this:

x = ""
def xy():
    global x = "String"

Yields an syntax error of invalid syntax. Why?

Thanks in advance for any help.

Druckermann
  • 681
  • 2
  • 11
  • 31
  • `global x` is just a declaration. Following a line with just that in it, you can then do the `x = "String"` to change its value. – martineau Mar 11 '19 at 22:07
  • 1
    If you search on the phrase "Python function variable scope", you’ll find resources that can explain it much better than we can in an answer here. – Prune Mar 11 '19 at 22:13
  • Possible duplicate of [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Ari Mar 11 '19 at 22:44

3 Answers3

2

I found the answer.

    x = ""
    def xy():
        global x 
        x = "String"

Yields the result I wanted.

Druckermann
  • 681
  • 2
  • 11
  • 31
  • 2
    I'm glad you solved your problem! It's generally ill advised to use `global` due to name space collisions though. It's typically preferable to pass your variable into your function. – Reedinationer Mar 11 '19 at 22:10
  • 1
    Thank you, I realize that this is bad practice and will likely refactor that soon – Druckermann Mar 11 '19 at 22:12
1

Why not try passing the variable to a modifying function:

x = ''
def xy(x):
    x += "String"
    return x
x = xy(x)
print(x)

This defines a function that takes an input x and then returns it after modifying it. This modified value is then reassigned to the x that is outside of scope. Perhaps a more clear example would look like

x = 'My input' # create an initial variable
def xy(my_input):
    my_input += " String" # append another string to our input string
    return my_input # give the modified value back so we can overwrite old value
x = xy(x) # reassign the returned value of the function to overwrite this variable
print(x)

Outputs:

My input String

Hopefully this illustrates that a local function can modify a value if it is input to the function. This modified value should then be returned and used to overwrite the old value. This technique not only allows you to pass global variables to other functions for modification, but allows local variables to be passed to functions for modification as well.

Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • Your functions do not modify anything. The parameter is completely ignored in both cases, as you immediately create a local variable with the same name. Both of your functions are exactly equivalent to `def xy(): return "String"`. The only reason why `x` is modified is because you assign to it the return value of the function. This answer is very misleading. – Thierry Lathuille Mar 11 '19 at 22:37
  • @ThierryLathuille hmm you do have a point. I've changed it to `+=` so it appends to whatever is input to the function instead of returning a value that is independent of the input like the original version – Reedinationer Mar 11 '19 at 22:39
  • It still doesn't modify the variable you passed as a parameter. The only reason why `x` is modified in the end is because you do `x = ...` out of the function. Just call `xy(x)` without assigning the result back to `x` and you'll see that it is left unchanged. – Thierry Lathuille Mar 11 '19 at 22:57
  • @ThierryLathuille thank you for the constructive feedback I've edited it again to emphasize that the value should be returned and then used to overwrite the original value. Is this more clear now? – Reedinationer Mar 11 '19 at 23:07
0

You can pass variables into a function, which is the best practice. You can return the result of a function to a variable as well. So instead of assigning a value to x inside the function, you can just use return to send the variable back, see below:

def xy(input):
    output = ''
    if input == 'Empty':
        output = 'Full'
    else:
        output = 'Empty'
    return output


x = 'Empty'
print(x)
>>> 'Empty'

# Update the result using xy()
x = xy(x)
print(x)
>>> 'Full'

# Once more
x = xy(x)
print(x)
>>> 'Empty'
Ari
  • 5,301
  • 8
  • 46
  • 120