[Python-ideas] The "in"-statement

Joshua Landau joshua.landau.ws at gmail.com
Mon Nov 5 23:05:33 CET 2012

On 5 November 2012 19:47, Bruce Leban <bruce at leapyear.org> wrote:

> 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?

My solution has always been:

.third_attr = lambda: .first_attr
.fourth_attr = not_attr

Although a single "." is hard for some [weaklings; pah!] to see.

It has exactly one use-case in my opinion:
def m(self, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u,
v, w, x, y, z):
    in self:
        .a, .b, .c, .d, .e, .f, .g, .h, .i, .j, .k, .l, .m, .n, .o, .p, .q,
.r, .s, .t, .u, .v, .w, .x, .y, .z = \
            a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u,
v, w, x, y, z

....not that having an excuse to do this is a good thing...

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")

TYVM for this, I'd never have thought of it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121105/6ff0e596/attachment.html>

More information about the Python-ideas mailing list