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


Groups > comp.lang.python > #90218

Re: Is this unpythonic?

From Gregory Ewing <greg.ewing@canterbury.ac.nz>
Newsgroups comp.lang.python
Subject Re: Is this unpythonic?
Date 2015-05-09 19:47 +1200
Message-ID <cr5sc6FgfmiU1@mid.individual.net> (permalink)
References (1 earlier) <554c8b0a$0$12992$c3e8da3$5496439d@news.astraweb.com> <mailman.231.1431082398.12865.python-list@python.org> <554cd119$0$12977$c3e8da3$5496439d@news.astraweb.com> <mik7j6$59n$1@ger.gmane.org> <mailman.273.1431154310.12865.python-list@python.org>

Show all headers | View raw


Frank Millman wrote:
> There are two operations I might perform on the dictionary -
> 
> 1. iterate over the keys and retrieve the values
> 
> 2: use 'in' to test if a given string exists as a key
> 
> Both of these operations will work on a tuple and give the desired result, 
> so it is a very valid workaround.

Although if I were reviewing a function like that,
it would probably make me pause for a moment or two
to consider why a tuple was being used as a value
for a parameter that is ostensibly supposed to be
a dict, and convince myself that it was okay.

The absolutely clearest way to write it would
probably be

    def f(things = None):
       "things is a mapping of stuff to be operated on"
       if things:
          for key in things:
             value = things[key]
             ...

A default value of None is a well-established idiom
for "this parameter is optional", and "if x:" is
idiomatic for "if I've been given an x", so writing it
that way has the best chance of passing the "pretty much
what you expect" test for good code. :-)

-- 
Greg

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


Thread

Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 10:01 +0200
  Re: Is this unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-08 20:08 +1000
    Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 12:53 +0200
      Re: Is this unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-09 01:07 +1000
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 07:56 +0200
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 08:51 +0200
          Re: Is this unpythonic? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-05-09 19:47 +1200
            Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 11:10 +0200
    Re: Is this unpythonic? Dave Angel <davea@davea.name> - 2015-05-08 08:04 -0400
      Re: Is this unpythonic? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-05-10 10:04 +0200
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-10 10:58 +0200
          Re: Is this unpythonic? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-05-10 11:54 +0200
            Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-10 12:40 +0200
    Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 15:12 +0200

csiph-web