contextlib.nested()

brasse thebrasse at gmail.com
Fri Nov 7 05:18:02 EST 2008


On Nov 7, 10:33 am, Peter Otten <__pete... at web.de> wrote:
> brasse wrote:
> > with nested(Foo('a'), Foo('b', True)) as (a, b):
> >     print a.tag
> >     print b.tag
>
> If been watching this thread for a while, and I think that your problems
> will go away if you write actual nested with-blocks:
>
> with Foo("a") as a:
>     with Foo("b") as b:
>         print a.tag
>         print b.tag
>
> Why look for a complex solution if there is a simple one?
>

That works great if you are only working with two objects. It gets a
bit uglier when you need to use three or more objects. I'm just trying
to figure out if there is some kind of best practice in the Python
community that works well (even with more than two objects) for the
two usage scenarios I have described.

:.:: mattias



More information about the Python-list mailing list