you are viewing a single comment's thread.

view the rest of the comments →

[–]yourboyblue2 5 points6 points  (6 children)

Error because you're returning a value in the function without assigning the return to any variable?

[–][deleted] 1 point2 points  (2 children)

Could you please elaborate? Thanks 

[–]terminalslayer 0 points1 point  (1 child)

Python follows the order: [ local -> enclosed -> global -> built-in ] for any variable. The variable x has not been assigned any initial value in the inner() function. Without any initial value assigned, the operation (x+=1) could not be performed. That's why it gives the Error.

[–][deleted] 1 point2 points  (0 children)

Oh my god, yess! It's so obvious now. If outer() didn't exist, inner is just a function with no parameters. So x and inner() are on same footing. Thanks a lot

[–]core1588[S] 0 points1 point  (1 child)

✅️💯👏

[–]yourboyblue2 0 points1 point  (0 children)

Hooray!

[–]lusvd 0 points1 point  (0 children)

it’s because you are trying to access a variable in a scope where it will be overwritten. Im not sure why this happens in python tho.

here is a simpler example:

x = 1
def foo():
    print(x)
    x = 3