On Jan 19, 6:02 pm, "David Tweet" <[EMAIL PROTECTED]> wrote:
> Hello,
>
> Seems to me that setattrs sort of assumes that you want to have all your
> initialization arguments set as attributes of the same name. I would think
> you'd sometimes want to be able to process the extra arguments inside of
[EMAIL PROTECTED] a écrit :
>> Grab(argdict, key, default) is argdict.pop(key, default)
>
> "pop() raises a KeyError when no default value is given and the key is
> not found."
Then use it with a default value !-)
>> def grab(kw, key, default=None):
>>try:
>> return kw.pop(key)
>>ex
On Jan 21, 10:09 am, [EMAIL PROTECTED] wrote:
> > Grab(argdict, key, default) is argdict.pop(key, default)
>
> "pop() raises a KeyError when no default value is given and the key is
> not found."
And it doesn't if a default is provided, which is always the case in
the uses of Grab(...), so it seem
> Grab(argdict, key, default) is argdict.pop(key, default)
"pop() raises a KeyError when no default value is given and the key is
not found."
> def grab(kw, key, default=None):
>try:
> return kw.pop(key)
>except KeyError:
> return default
So Bruno's technique seems to me to be
David Tweet a écrit :
(please, don't top-post)
>
> def Grab(argdict, key, default):
cf pep08 for naming conventions...
> """Like argdict.get(key, default), but also deletes key from argdict."""
> if key in argdict:
> retval = argdict["key"]
> del(argdict[key])
> else:
> retval
Ah! nice, thanks, knew I was probably missing something.
On Jan 19, 2008 5:01 PM, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
> On Jan 19, 11:02pm, "David Tweet" <[EMAIL PROTECTED]> wrote:
>
> > def Grab(argdict, key, default):
> > """Like argdict.get(key, default), but also deletes key from argdi
On Jan 19, 11:02 pm, "David Tweet" <[EMAIL PROTECTED]> wrote:
> def Grab(argdict, key, default):
> """Like argdict.get(key, default), but also deletes key from argdict."""
> if key in argdict:
> retval = argdict["key"]
> del(argdict[key])
> else:
> retval = default
> return ret
Hello,
Seems to me that setattrs sort of assumes that you want to have all your
initialization arguments set as attributes of the same name. I would think
you'd sometimes want to be able to process the extra arguments inside of each
__init__, assign them to attributes with different names, etc.
A situation that often comes up is having to initialize several
instance attributes that accept a default value. For a single class,
passing the default values in __init__ is fine:
class Base(object):
def __init__(self, x=0, y=None):
self.x = x
self.y = y
For i