I wonder why with open file I do not get indentation problem

Hi I have been learning python. When I was learning about files I have come across this. It is not a problem but I wonder why there is no error thrown

example

with open('just_the_first.txt') as first_line_doc:
  first_line = first_line_doc.readline()
print(first_line)

As first_line variable is declared into with open and should be scoped into with open. But I am printing that variable outside of its scope why do I not get an - Indentation Error?

The with keyword doesn’t actually create a subscope, unlike a function block, class, etc., and therefore you can access a variable whose definition is indented under a with statement as if it were defined the parent scope of the with statement.

3 Likes