Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with a global variable inside a python function

Tags:

python

At this code snippet:

def taylorVazquez(fx,a,b,n,puntos):

    puntosX = linspace(a,b,num=puntos)
    aproxY = []
    puntosY = []    

    def toFloat():
        puntosX = [float(x) for x in puntosX]
        aproxY = [float(y) for y in aproxY]
        puntosY = [float(y) for y in puntosY]

I'm getting the error message:

UnboundLocalError: local variable 'puntosX' referenced before assignment

And so I know same would happen to the other two variables. What can I do it's the outer taylorVazquez's variables the ones I manipulate at inner function, in other words, I want the assignments to work for the outer scope too

like image 208
diegoaguilar Avatar asked May 24 '26 19:05

diegoaguilar


1 Answers

Whenever you assign a value to a variable in a given scope, the variable is assumed to be local to that scope. Depending on which Python you are using, you will either need to use nonlocal (Python 3) or pass the values in using parameters (Python 2).

Here is Python 2:

def toFloat(puntosX, aproxY, puntosY):  # now the names of the enclosing function can be passed to the toFloat function
    puntosX = [float(x) for x in puntosX]
    aproxY = [float(y) for y in aproxY]
    puntosY = [float(y) for y in puntosY]

In Python 3:

def toFloat():
    nonlocal puntosX, aproxY, puntosY  # the names now refer to the enclosing scope rather than the local scope
    puntosX = [float(x) for x in puntosX]
    aproxY = [float(y) for y in aproxY]
    puntosY = [float(y) for y in puntosY]

global will NOT work in this situation, since you are referencing the names of an enclosing function.

One more thing, you MAY be trying to assign new values to the names in the enclosing scope. Your present tactic will not work, since you are assigning new objects to these names within the innermost function. (List comprehensions create new lists.) If you need to keep the new values, you will need to (for example) return these values to the enclosing scope and reassign your original names to the new values. For example, in Python 2:

def taylorVazquez(fx,a,b,n,puntos):
    puntosX = linspace(a,b,num=puntos)
    aproxY = []
    puntosY = [] 

    def toFloat(puntosX, aproxY, puntosY):  # now the names of the enclosing function can be passed to the toFloat function
        puntosX = [float(x) for x in puntosX]
        aproxY = [float(y) for y in aproxY]
        puntosY = [float(y) for y in puntosY]
        return puntosX, aproxY, puntosY

    puntosX, aproxY, puntosY = toFloat(puntosX, aproxY, puntosY)  # now you can reassign these names to the new values

Unlike global, you cannot assign new values to these names and have them hold for the enclosing scope.

like image 66
Justin O Barber Avatar answered May 26 '26 09:05

Justin O Barber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!