[Python-ideas] The "in"-statement

Bruce Leban bruce at leapyear.org
Mon Nov 5 20:47:12 CET 2012


On Mon, Nov 5, 2012 at 11:28 AM, Markus Unterwaditzer <
markus at unterwaditzer.net> wrote:

> While mocking objects, i got annoyed by the following code pattern i had to
> use when modifying multiple attributes on a single object::
>
>     obj.first_attr = "value"
>     obj.second_attr = "value2"
>
>     some_other = "lel"
>
> I thought it would be neat if i could do::
>
>     in obj:
>         first_attr = "value"
>         second_attr = "value2"
>
>     some_other = "lel"  # indenting this would cause it to appear as an
> attribute of obj
>

Hard to read, error-prone and ill-defined. Does it create new attributes or
only change existing ones? What about identifiers on right hand sides? What
would
    third_attr = lambda: first_attr
do?

And this is easy enough:

def multi_setattr(obj, **kwargs):
    for k in kwargs:
        setattr(obj, k, kwargs[k])

multi_setattr(obj,
    first_attr = "value",
    second_attr = "value2")

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121105/c22b990b/attachment.html>


More information about the Python-ideas mailing list