Recommended way to unpack keyword arguments using **kwargs ?

2012-10-26 Thread Jeff Jeffries
I have been doing the following to keep my class declarations short:

class MyClass(MyOtherClass):
def __init__(self,*args,**kwargs):
self.MyAttr = kwargs.get('Attribute',None) #To get a default
MyOtherClass.__init__(self,*args,**kwargs)

Is there a recommended way to get keyword arguments (or default) when using
** notation?

-- 
Cheerios,
Jeff Jeffries III
CFO: www.touchmycake.com http://www.willyoubemyfriend.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recommended way to unpack keyword arguments using **kwargs ?

2012-10-26 Thread Ian Kelly
On Fri, Oct 26, 2012 at 10:58 AM, Jeff Jeffries
jeff.jeffries@gmail.com wrote:
 I have been doing the following to keep my class declarations short:

 class MyClass(MyOtherClass):
 def __init__(self,*args,**kwargs):
 self.MyAttr = kwargs.get('Attribute',None) #To get a default
 MyOtherClass.__init__(self,*args,**kwargs)

 Is there a recommended way to get keyword arguments (or default) when using
 ** notation?

It's better form to use .pop() instead of .get(), as if MyClass is
receiving the argument then MyOtherClass is probably not expecting it.

If using Python 3, you can use the keyword-only argument syntax:

def __init__(self, *args, attribute=None, **kwargs):
# Do something with attribute
MyOtherClass.__init__(self, *args, **kwargs)


Either way, you should really only do this when there are a large
number of possible arguments or you don't really know what arguments
to expect.  When practical to do so, it is preferable to explicitly
list the arguments so that the method signature can be inspected, e.g.
by the help() command.
-- 
http://mail.python.org/mailman/listinfo/python-list