Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api.': 0.05; 'explicitly': 0.05; 'none:': 0.07; 'purpose.': 0.07; 'caller': 0.09; 'default.': 0.09; 'indicates': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; '0),': 0.16; 'fine.': 0.16; 'finney': 0.16; 'object()': 0.16; 'parameter.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sentinel': 0.16; 'set,': 0.16; 'singleton': 0.16; 'stallman': 0.16; 'subject:default': 0.16; 'value.': 0.19; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'specify': 0.24; 'compare': 0.26; 'header:X-Complaints-To:1': 0.27; 'specifically': 0.29; 'serve': 0.31; 'url:wiki': 0.31; 'subject:that': 0.31; 'url:wikipedia': 0.31; 'writes:': 0.31; 'class': 0.32; 'reader': 0.33; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'problem': 0.35; 'case,': 0.35; 'there': 0.35; 'choosing': 0.36; 'mass': 0.36; 'subject:one': 0.36; "didn't": 0.36; 'url:org': 0.36; 'behind': 0.37; 'ben': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'new': 0.61; 'simple': 0.61; 'name': 0.63; 'choose': 0.64; 'different': 0.65; 'chance': 0.65; 'entitled': 0.65; 'between': 0.67; 'default': 0.69; 'distinguish': 0.84; 'domain,': 0.84; 'received:125': 0.84; 'shield': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: How to detect that a function argument is the default one Date: Fri, 12 Dec 2014 10:03:28 +1100 References: <5488634e$0$2130$426a74cc@news.free.fr> <5489f45d$0$53762$c3e8da3$3304c218@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:TwKSNEcT6YSLkoWSo+n/dCFYYAo= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418339026 news.xs4all.nl 2875 [2001:888:2000:d::a6]:60880 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82394 Tony the Tiger writes: > radius='10', mass='1' > > if radius == '10' ... > > if mass == '1' ... This ignores the problem as stated: The OP wants to distinguish between a value that was explicitly set by the caller, versus a value that was set by default because the caller did not specify the parameter. The general solution to this is to choose a value outside the domain, a which is different from any value the caller would choose for that parameter. Often, the Python singleton ‘None’ is good for the purpose. I think in this case, where the domain is numeric, a default of ‘None’ would do fine. class Foo: def __init__(centre=(0, 0), radius=None, mass=None): if radius is None: # Caller didn't specify. Set a default value. radius = 10 if mass is None: # Caller didn't specify. Set a default value. mass = 1 self.centre = centre self.radius = radius self.mass = mass In some other cases, ‘None’ is a valid value for the caller to set, and so cannot be used as a sentinel to distinguish the default. For those cases, creating a specific object instance to serve as the sentinel is simple and clear: class Foo: _SENTINEL = object() def __init__(centre=(0, 0), radius=_SENTINEL, mass=_SENTINEL): if radius is _SENTINEL: # Caller didn't specify. Set a default value. radius = 10 if mass is _SENTINEL: # Caller didn't specify. Set a default value. mass = 1 self.centre = centre self.radius = radius self.mass = mass Creating a new object specifically for the sentinel means there is no chance some other value will compare identical. Choosing a leading-underscore name indicates to the reader that this is not part of the public API. -- \ “Odious ideas are not entitled to hide from criticism behind | `\ the human shield of their believers' feelings.” —Richard M. | _o__) Stallman | Ben Finney