with statements does not delete the object

Ben Finney ben+python at benfinney.id.au
Thu Sep 22 02:01:05 EDT 2011

yves at zioup.com writes:

> Is this the expected behaviour:

You can learn the expected behaviour for ‘with’ in the documentation

>     with mylib.token() as t:
>       do_something
>     dir()
> In the last dir(), after the with "loop" is finished, t still shows up... I
> expected it to be unreferenced by then.

What gives you that expectation, when it's not the case for any of ‘if’,
‘for’, ‘while’, ‘try’, and so on?

The set of statements which introduce a new scope is small, and ‘with’
is not one of them.

 \             “No smoothen the lion.” —lion cage, zoo, Czech Republic |
  `\                                                                   |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list