[Python-Dev] 'With' context documentation draft (was Re: Terminology for PEP 343

Phillip J. Eby pje at telecommunity.com
Thu Jul 7 20:19:28 CEST 2005


At 09:12 PM 7/6/2005 +1000, Nick Coghlan wrote:
>Another example is the use of contexts to handle insertion of the
>appropriate tags when generating HTML:
>
>     with html:
>        with body:
>           with h1:
>              print "Some heading"
>           with p:
>              print "This is paragraph 1"
>           with p:
>              print "This is paragraph 2"
>           with h2:
>              print "Another heading"

I suggest changing this to something like this:

     class tag(object):
         def __init__(self,name):
             self.name = name
         def __enter__(self):
             print "<%s>" % name
         def __exit__(self):
             print "</%s>" % name

     with tag('html'):
         # ... etc.

So that it's obvious where the implementation is coming from.  Otherwise, 
it looks altogether too magical.

Also, the posted draft doesn't explain what happens to the __enter__ return 
value, either in a literal sense or in the sense of where it fits in the 
overall concept of context management.



More information about the Python-Dev mailing list