Change in return position throws variable not defined error
Question:
I came across this problem while learning how to return a sorted list in Python. When I write the code this way
def func(A):
return A
A = sorted([4,5,6,7,2,1])
print(A)
It sorts the list perfectly and returns [1,2,3,4,5,6,7]
.
But when I change the position of return A
to
def func(A):
A = sorted([4,5,6,7,2,1])
return A
print(A)
It throws name 'A' is not defined
error.
I Googled why it’s showing variable not defined, and the first answer was to see whether I haven’t misspelled it and accessed it after defining it, and that I did.
Answers:
Trying to summarize what has been hinted to you in the comments using examples.
the function is never executed
Your code:
def func(A):
return A
A = sorted([4,5,6,7,2,1])
print(A)
Is equivalent to:
A = sorted([4,5,6,7,2,1])
print(A)
Although defined, the function is never used, so you could write:
def func(A): # defined but never executed
return 'unicorn' #
A = sorted([4,5,6,7,2,1])
print(A)
This wouldn’t change a thing.
Now doing the same with your second code:
def func(A): # defined
A = sorted([4,5,6,7,2,1]) # but never
return A # executed
print(A)
becomes:
print(A)
and python rightfully asks: "What is A?"
functions have a different scope
You should avoid using the same variable names inside and outside of a function. You can, and python is very good at knowing which one’s which, but humans not as much, so be explicit.
What you likely want is:
def func(lst): # func takes one parameter, lst
return sorted(lst) # does stuff on it and returns an output
A = [4,5,6,7,2,1]
print(func(A))
adding confusion: global scope and mutability
Now, functions can also access the outer scope, provided the variable is not also defined inside the function (property 1) and modify an external object if it is mutable (property 2).
Here is an example, but you should really avoid this kind of structure as it is unexplicit:
def func():
A.sort() # A is undefined in the function
# let's try to grab it from the outer scope
A = [4,5,6,7,2,1]
func() # now A is sorted
print(A)
# [1, 2, 4, 5, 6, 7]
I came across this problem while learning how to return a sorted list in Python. When I write the code this way
def func(A):
return A
A = sorted([4,5,6,7,2,1])
print(A)
It sorts the list perfectly and returns [1,2,3,4,5,6,7]
.
But when I change the position of return A
to
def func(A):
A = sorted([4,5,6,7,2,1])
return A
print(A)
It throws name 'A' is not defined
error.
I Googled why it’s showing variable not defined, and the first answer was to see whether I haven’t misspelled it and accessed it after defining it, and that I did.
Trying to summarize what has been hinted to you in the comments using examples.
the function is never executed
Your code:
def func(A):
return A
A = sorted([4,5,6,7,2,1])
print(A)
Is equivalent to:
A = sorted([4,5,6,7,2,1])
print(A)
Although defined, the function is never used, so you could write:
def func(A): # defined but never executed
return 'unicorn' #
A = sorted([4,5,6,7,2,1])
print(A)
This wouldn’t change a thing.
Now doing the same with your second code:
def func(A): # defined
A = sorted([4,5,6,7,2,1]) # but never
return A # executed
print(A)
becomes:
print(A)
and python rightfully asks: "What is A?"
functions have a different scope
You should avoid using the same variable names inside and outside of a function. You can, and python is very good at knowing which one’s which, but humans not as much, so be explicit.
What you likely want is:
def func(lst): # func takes one parameter, lst
return sorted(lst) # does stuff on it and returns an output
A = [4,5,6,7,2,1]
print(func(A))
adding confusion: global scope and mutability
Now, functions can also access the outer scope, provided the variable is not also defined inside the function (property 1) and modify an external object if it is mutable (property 2).
Here is an example, but you should really avoid this kind of structure as it is unexplicit:
def func():
A.sort() # A is undefined in the function
# let's try to grab it from the outer scope
A = [4,5,6,7,2,1]
func() # now A is sorted
print(A)
# [1, 2, 4, 5, 6, 7]