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


Groups > comp.lang.python > #82287

Re: How to detect that a function argument is the default one

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <skip.montanaro@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; 'python,': 0.02; 'else:': 0.03; 'read.': 0.03; 'value,': 0.04; 'guido': 0.05; 'defaults': 0.07; 'detect': 0.07; 'explicit': 0.07; 'level,': 0.07; 'none:': 0.07; 'tries': 0.07; 'python:': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; "wouldn't": 0.14; '"is"': 0.16; '(small)': 0.16; 'ast': 0.16; 'gained': 0.16; 'peters': 0.16; 'singleton': 0.16; 'stuff.': 0.16; 'subject:default': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; '>>>': 0.22; 'programming': 0.22; 'import': 0.22; 'comparing': 0.24; 'skip': 0.24; 'skip:_ 20': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'correct': 0.29; 'am,': 0.29; 'generally': 0.29; 'tim': 0.29; 'dec': 0.30; 'especially': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'clever': 0.31; 'subject:that': 0.31; 'this.': 0.32; 'maybe': 0.34; 'subject:the': 0.34; 'could': 0.34; 'basic': 0.35; 'something': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'mass': 0.36; 'subject:one': 0.36; 'too': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; "you're": 0.61; 'real': 0.63; 'pick': 0.64; 'more': 0.64; 'default': 0.69; 'zen': 0.84; 'write:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=HfzNY6fOJxxzjqjBxB1HKCOTRITgo3+9bNlQaoCHF/U=; b=VHvpumCRQp5eV9RS49Sw0CVrRQ8uH1vSOpwFpqX21FPoUKZBtOyyS7VCGUNS/RJJww vrMsMeHJEfjKpSKQ9IMMwbyBtoet0MIGdZ5qkBIKnIZQjMC+CeeagrXpHsmQngodI0La qxyngehknGeJyXB4WPaZ/SggBuJTyK7IWlZQn0Ac1WZbQDiZxEOREGkWUZexRk7hAICq R/1VyuAstH52/FWSPb4opCuybYdSVX0I8hBbxc+nsqeurBzHKzRWLBJV7QJy1927u31Z eKYaNdAzDI5JdCyjLXCn8RZQ5JmEePUT1/O8u+MLaeL5vbta6ELsQBbzw6/wzk0LkKr8 u+Bw==
MIME-Version 1.0
X-Received by 10.52.78.166 with SMTP id c6mr2655780vdx.88.1418225374081; Wed, 10 Dec 2014 07:29:34 -0800 (PST)
In-Reply-To <5488634e$0$2130$426a74cc@news.free.fr>
References <5488634e$0$2130$426a74cc@news.free.fr>
Date Wed, 10 Dec 2014 09:29:34 -0600
Subject Re: How to detect that a function argument is the default one
From Skip Montanaro <skip.montanaro@gmail.com>
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16809.1418225382.18130.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418225382 news.xs4all.nl 2935 [2001:888:2000:d::a6]:55996
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82287

Show key headers only | View raw


On Wed, Dec 10, 2014 at 9:14 AM, ast <nomail@invalid.com> wrote:
> I have the idea to write:
>
> def __init__(center=(0,0), radius=10, mass=None)):
>
>    if  mass == None:        self.mass = radius**2
>    else:
>        self.mass = mass
>
> but maybe Python provides something clever.

This is almost the correct idiom. In general, pick defaults from
values outside your valid domain. None is often perfect for
this. Since you're comparing to a singleton value, "is" is the usual
comparison, so your code becomes:

def __init__(center=(0,0), radius=10, mass=None)):
    if mass is None:
        self.mass = radius**2
    else:
        self.mass = mass

At the programming level, Python generally tries not to be too clever,
especially for real basic stuff. From the Zen of Python:

    >>> import this
    The Zen of Python, by Tim Peters

    ...
    Explicit is better than implicit.
    ...

Guido could have been more clever here, but it wouldn't have likely
gained much (you'd still have to detect the default and set the value
appropriately), and perhaps made the code a (small) bit harder to
read.

Skip

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


Thread

How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 16:14 +0100
  Re: How to detect that a function argument is the default one Skip Montanaro <skip.montanaro@gmail.com> - 2014-12-10 09:29 -0600
    Re: How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 16:45 +0100
    Re: How to detect that a function argument is the default one Marko Rauhamaa <marko@pacujo.net> - 2014-12-10 18:33 +0200
  Re: How to detect that a function argument is the default one Chris Angelico <rosuav@gmail.com> - 2014-12-11 03:16 +1100
    Re: How to detect that a function argument is the default one wxjmfauth@gmail.com - 2014-12-10 12:08 -0800
    Re: How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 22:17 +0100
      Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-11 11:03 -0700
      Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-11 11:07 -0700
  Re: How to detect that a function argument is the default one Ned Batchelder <ned@nedbatchelder.com> - 2014-12-10 11:19 -0500
  Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-10 09:20 -0700
  Re: How to detect that a function argument is the default one Tony the Tiger <tony@tiger.invalid> - 2014-12-11 19:45 +0000
    Re: How to detect that a function argument is the default one Ben Finney <ben+python@benfinney.id.au> - 2014-12-12 10:03 +1100

csiph-web