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

Reply via email to