-2

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?

Hamza.S
  • 1,319
  • 9
  • 18

1 Answers1

0
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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 08 '22 at 09:03