1

I have a little question about variables. My main language is Java (and I'm learning Python) so, I have a problem calling a variable in a function, it doesn't refresh its new value:

# Values
global e1x, e1y, e1c, e2x, e2y, e2c, altx, alty, x, y

def getValues():
    print("Taking Ax + By = C:")
    e1x = float(input("Value of x in first equation: "))
    #...
    if(confirm()): # A function I ommited 'cause its irrelevant
        return e1x, e1y, e1c, e2x, e2y, e2c
    else:
        getValues()

def calculateValues():
    # Stuff with variables


# MAIN
getValues()
calculateValues()

I tried to write it without the global, tried to use the self word but, It doesn't work. (Using Python 3)

ERROR:

 Traceback (most recent call last):
   File "E002_GaussSeidel.py", line 41, in <module>
      calculateValues()
   File "E002_GaussSeidel.py", line 34, in calculateValues
      print(str(e1x))
 NameError: name 'e1x' is not defined
  • 1
    Burn *global e1x, e1y, e1c, e2x, e2y, e2c, altx, alty, x, y*. You don't really want to use globals. You would also use `self` in a class method where self refers to the instance the method would be called on. If you want to use that logic then actually create a class not just functions. – Padraic Cunningham Sep 28 '16 at 22:07
  • "It doesn't work" is not a problem description. Do you get an error? If so, what's the full traceback? Does it behave unexpectedly? If so, describe the expected and actual behaviour. – Sven Marnach Sep 28 '16 at 22:07
  • Python says "e1x is not defined" when I try to use it on 'calculateValues()' but I declared on top and initialized on the function which actually returns those values @SvenMarnach – Alfonso Izaguirre Martínez Sep 28 '16 at 22:10
  • @AlfonsoIzaguirreMartínez You are supposed to move the global decl to the top of `getValues()`. but you don't want to use global variables because they are evil. – noɥʇʎԀʎzɐɹƆ Sep 28 '16 at 22:12
  • @AlfonsoIzaguirreMartínez This should be included in your question, including the full traceback of the error. You can't declare variables in Python, and `global` outside a function definition doesn't do anything. – Sven Marnach Sep 28 '16 at 22:12
  • The `global` statement only applies to *functions*. Python determines that a name is a local in a function if you *bind to it* (assignment is one form of binding). The `global` statement tells Python to override that determination. You didn't use `global` in the function, you used it elsewhere. – Martijn Pieters Sep 28 '16 at 22:15
  • the duplicate is not what you want. because global variables are evil – noɥʇʎԀʎzɐɹƆ Sep 28 '16 at 22:22

1 Answers1

2

You need to include global within your function. Outside it does nothing.

def getValues():
    global e1x, e1y, e1c, e2x, e2y, e2c, altx, alty, x, y
    print("Taking Ax + By = C:")
    e1x = float(input("Value of x in first equation: "))
    #...
    if(confirm()): # A function I ommited 'cause its irrelevant
        return e1x, e1y, e1c, e2x, e2y, e2c
    else:
        getValues()

def calculateValues():
    # Stuff with variables


# MAIN
getValues()
calculateValues()

But why do you need global variables? Are you going to use those variables outside your function? global is only necessary if you need to modify values that exist outside the scope of the function.

Reformat your code to be like:

def getValues():
    print("Taking Ax + By = C:")
    e1x = float(input("Value of x in first equation: "))
    #...
    if(confirm()): # A function I ommited 'cause its irrelevant
        return e1x, e1y, e1c, e2x, e2y, e2c
    else:
        getValues()

def calculateValues(values):
    # Stuff with variables


# MAIN
calculateValues(getValues())

Instead of passing information with global variables, this passes information by return values. There are hundreds of articles on why global variables are evil.

values holds the returned variables e1x, e1y, e1c, e2x, e2y, e2c. It is accessible using list index notation. If you want to refer to the variables by their names, use:

#...
def calculateValues(e1x, e1y, e1c, e2x, e2y, e2c):
    # Stuff with variables


# MAIN
calculateValues(*getValues())

*foo is list unpacking notation. It is an advanced topic, but it is useful in your situation. You can read more about list unpacking here.

Community
  • 1
  • 1
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67