简体   繁体   中英

error while assigning a new value to a variable

I was working with scope in python

a = 10
{
    a = 33
}

and I got this error,

File "", line 3 a= 33 ^ SyntaxError: invalid syntax

so it means block scope is only for functions in python?

def foo():
    a = 33
    print(f"Inside: {a}") # Inside: 33

a = 10
foo()
print(f"Outside: {a}") # Outside: 10

I think this example of function scope is the only way to get something like block scope in Python. Idea taken from OP and confirmed in Block scope in Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM