A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2004-March/043234.html below:

[Python-Dev] redefining is

[Python-Dev] redefining isBarry Warsaw barry at python.org
Thu Mar 18 11:48:29 EST 2004
On Thu, 2004-03-18 at 11:45, Aahz wrote:
> On Thu, Mar 18, 2004, Barry Warsaw wrote:
> > On Thu, 2004-03-18 at 10:23, Jewett, Jim J wrote:
> >> 
> >> There is an idiom (I've seen it more in Lisp than in python) 
> >> of creating a fresh object to act as a sentinel.
> > 
> > A very common use case in Python is where None is a valid value in a
> > dictionary:
> > 
> > missing = object()
> > if d.get('somekey', missing) is missing:
> >    # it ain't there
> > 
> > It even reads well!
> 
> Ugh.  While I agree that the idiom has its place, this ain't one of
> them; you should be using ``in`` (or ``has_key()``).  The standard idiom
> is even more readable, and there should be only one way to do it.  Maybe
> you meant something more like
> 
>     if d['somekey'] is missing:

Let me rephrase that:

missing = object()
value = d.get('somekey', missing)
if value is missing:
   # it ain't there
else:
   return value

-Barry



More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4