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


Groups > comp.lang.python > #82296

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: How to detect that a function argument is the default one
Date Wed, 10 Dec 2014 18:33:45 +0200
Organization A noiseless patient Spider
Lines 39
Message-ID <8761djjwgm.fsf@elektro.pacujo.net> (permalink)
References <5488634e$0$2130$426a74cc@news.free.fr> <mailman.16809.1418225382.18130.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="23801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19LYeKlbmkuTYo2jxZ5ycuO"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:C+/F5/vT0o5/mwWAHBRAvs3i/b0= sha1:r64WGtjWl9DgxfcY0r+yuxUMuRk=
Xref csiph.com comp.lang.python:82296

Show key headers only | View raw


Skip Montanaro <skip.montanaro@gmail.com>:

> 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

In this case, None works well. However, sometimes None is a valid input
value. Then, a sentinel object is the way out:

        
    _OMITTED = object()

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


Marko

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