How to set object parameters nicely?

inhahe inhahe at gmail.com
Thu Dec 3 09:33:22 EST 2009


On Thu, Dec 3, 2009 at 9:32 AM, inhahe <inhahe at gmail.com> wrote:
> On Thu, Dec 3, 2009 at 9:16 AM, Lie Ryan <lie.1296 at gmail.com> wrote:
>> On 12/2/2009 10:26 AM, allen.fowler wrote:
>>>
>>> I've tried this, but have found two issues:
>>>
>>> 1) I can't set default values.
>>> 2) I can't set required values.
>>>
>>> In both of the above cases, if the object is created without the
>>> "exact" dict() I expect, all the assumption my methods make about what
>>> is available in "self" fall apart.
>>>
>>
>
> def __init__(self, required1, required2, default1='d1', default2='d2',
> **kwargs):
>    for par in kwargs:
>       self.setattr(par, kwargs[par])
>   self.required1 = required1
>   self.required2 = required2
>   self.default1 = default1
>   self.default2 = default2
>
> or
>
> def __init__(self, required1, required2, default1='d1', default2='d2',
> **kwargs):
>    for par in kwargs:
>       self.setattr(par, kwargs[par])
>   self.required1 = required1
>   self.required2 = required2
>   self.default1 = default1
>   self.default2 = default2
>

(sorry, sent the same code twice. i was going to do it a different way
and realized it would be too magically)



More information about the Python-list mailing list