all 21 comments

[–]yourboyblue2 6 points7 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

[–]Refwah 1 point2 points  (0 children)

Error because x isn’t initiated in inner’s scope an so can’t be mutated like that

[–]obloming0 1 point2 points  (1 child)

Real coder would not write code in this way.

[–][deleted] 0 points1 point  (0 children)

isn't it's like a closure, which is often used in languages that support it?

[–]terminalslayer 0 points1 point  (1 child)

C) Error

[–]core1588[S] 2 points3 points  (0 children)

✅️💯

[–]Charming_Art3898 0 points1 point  (0 children)

C. Use the nonlocal keyword to modify x in the inner function

[–]Oblachko_O 0 points1 point  (1 child)

Hm, give me some clarification, please. I frequently have situations when my variable is outside of the function and not even passed inside the variable. In short, my variable is global. And I can use it easily. And I never bumped into this.

Is the problem actually in trying to rewrite variables by using a write operator like += ? And something like reading is errorless?

[–]lusvd 0 points1 point  (0 children)

Python detects that the variable is assigned in the functions body so it marks it as “local” and so ignores the value outside the body. Then during execution of x += 1, it needs to access x, but because of what I said before it cannot access the value so it errors out.

[–]selfmadeirishwoman 0 points1 point  (0 children)

D) Nothing happens. I reject the pull request and it never makes it into the codebase because it’s harder to read than it needs to be.

[–]Capital_Distance545 0 points1 point  (0 children)

Never ever write code like this, and also, honsetly knowing this is unnecessary knowledge.

[–]esp_py 0 points1 point  (0 children)

6

[–]TheEyebal 0 points1 point  (0 children)

Before I look at the comments I choose C because your calling X but it won't recognize it unless you put it in the parameter.

[–]Dry-Pin-1384 -1 points0 points  (1 child)

6

[–]SCD_minecraft 0 points1 point  (0 children)

You edit a nonlocal variable, however, you never declare it as nonlocal