[Python-ideas] The "in"-statement

Devin Jeanpierre jeanpierreda at gmail.com
Tue Nov 6 08:55:17 CET 2012


On Mon, Nov 5, 2012 at 3:29 PM, Mike Graham <mikegraham at gmail.com> wrote:
> On Mon, Nov 5, 2012 at 2:49 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
>> On 05.11.12 21:28, Markus Unterwaditzer wrote:
>>>
>>> I thought it would be neat if i could do::
>>>
>>>      in obj:
>>>          first_attr = "value"
>>>          second_attr = "value2"
>>
>>
>>     vars(obj).update(
>>         first_attr="value",
>>         second_attr="value2",
>>         )
>>
>> Or obj.__dict__.update.
>
> Tinkering with the object's attribute dict directly using either of
> these is extremely error-prone because it does not work for many
> objects and because it circumvents the descriptor protocol.

def update_vars(obj, **kwargs):
    for k, v in kwargs.iteritems():
        setattr(obj, k, v)

update_vars(obj,
    a=b,
    c=d)

-- Devin



More information about the Python-ideas mailing list