I seem to be somehow screwing up the most basic thing. I have:
def function(a, b, c):
return 'hi'
print(function(a,b,c)) results in a NameError for every variable.
What is the cause of this?
asked Apr 19, 2015 at 2:15
Sundrah
9151 gold badge9 silver badges16 bronze badges
1 Answer 1
The names of the arguments of a function are local variables, they are not available as global names. a, b and c exist only inside of the function, and receive the values you pass to the function.
You need to create new variables or use literal values when calling the function:
print(function(1, 2, 3))
would work because 1, 2 and 3 are actual values to pass into the function.
answered Apr 19, 2015 at 2:17
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py