Python "with"

Peter Otten __peter__ at web.de
Mon Sep 17 07:23:03 EDT 2007


Ivan Voras wrote:

> Well, no, but this might be due to personal tastes. At least, I don't
> think it's better then some other alternatives. For example, in C99 you
> can do:
> 
> static struct option_s foo_option = {
>      .name = "foo",
>      .type = O_STRING,
>      .def_value = "default"
> };
> 
> At least to me, this looks even better than the Pascal's syntax.

foo_option = OptionS(
    name="foo",
    type=O_STRING,
    def_value="default"
)

So doesn't the Python analog even look better than C? If so, you don't
need new syntax:

>>> def with_(obj, **update):
...     for nv in update.iteritems():
...             setattr(obj, *nv)
...
>>> with_(a.long.way.to.tipperary,
...     alpha=42,
...     beta="yadda",
...     gamma=None
... )
>>> a.long.way.to.tipperary
A(alpha=42, beta='yadda', gamma=None)

Peter



More information about the Python-list mailing list