Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32228

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'explicitly': 0.04; 'jeff': 0.04; 'attribute': 0.05; '*args,': 0.07; 'arguments': 0.07; 'python': 0.09; '**kwargs)': 0.09; '**kwargs):': 0.09; 'command.': 0.09; 'declarations': 0.09; 'default)': 0.09; 'preferable': 0.09; 'subject:using': 0.09; 'def': 0.10; 'oct': 0.16; 'wrote:': 0.17; '(or': 0.18; 'header:In- Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:_ 10': 0.29; 'probably': 0.29; 'class': 0.29; 'e.g.': 0.30; 'fri,': 0.30; 'keyword': 0.30; 'subject: ?': 0.30; 'skip:_ 30': 0.32; 'to:addr:python-list': 0.33; 'recommended': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'skip:k 20': 0.35; 'so,': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'really': 0.36; 'skip:m 40': 0.36; 'method': 0.36; 'should': 0.36; 'possible': 0.37; 'signature': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; '26,': 0.65; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=ExeNbS7+ulF8PUaX1Qtq8ga7TJOYRqhg6R/fOT8fcVU=; b=ZR92LDZV6gnEzh7QL6KRjyx3XuEmXKpDLm7cLp9PSOoUv/MglpEaNHa8y1X49S0Rau NFifoL93msZpumnzRrN9rMqDm5RQph0EioSPDQ3MKpdai3uEJyHI5ZhRsz0A/AePn2SN l1JXCiAGYX4Wog9/C2kG5vk97XXNADtWg3QbWzy5nHdNV4vH+3oe+CIxrQ1oKINXi1Xt sSDXaWcoGrbk3NQokHbVaG76MdcuKTVCWb5a5aIq+w7T5NUeJzIjd5bEOXPW4TBRQPtj lf4ASpej7XSrh7e1lIo9BK22onNZe/YYpqcdI8xzpPF498h5mPgFfH505FPMhk+v7HTu y2Zw==
MIME-Version 1.0
In-Reply-To <CANNB2xteri_DDGGVt-OvkU0mj1LhNBMz5wnctEqJmhorwQqeOQ@mail.gmail.com>
References <CANNB2xteri_DDGGVt-OvkU0mj1LhNBMz5wnctEqJmhorwQqeOQ@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 26 Oct 2012 11:13:05 -0600
Subject Re: Recommended way to unpack keyword arguments using **kwargs ?
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2900.1351271618.27098.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351271618 news.xs4all.nl 6909 [2001:888:2000:d::a6]:49182
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32228

Show key headers only | View raw


On Fri, Oct 26, 2012 at 10:58 AM, Jeff Jeffries
<jeff.jeffries.iii@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Recommended way to unpack keyword arguments using **kwargs ? Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-26 11:13 -0600

csiph-web