Is this unpythonic?

Frank Millman frank at chagford.com
Sat May 9 05:10:00 EDT 2015


"Gregory Ewing" <greg.ewing at canterbury.ac.nz> wrote in message 
news:cr5sc6FgfmiU1 at mid.individual.net...
> Frank Millman wrote:
>
> 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. :-)
>

Thanks, Greg, that makes a lot of sense.

My original method of using 'z=[]' worked, but would cause a reviewer to 
stop and think about it for a moment.

Changing it to 'z=()' would have pretty much the same effect.

Using 'z=None' would cause the least hesitation, and is therefore I think 
the most pythonic.

It does require an additional test every time the function is called, but 
the effect of that in my application is virtually zero.

If the overhead did become an issue, Dave's suggestion of 'z=EMPTY_LIST' 
would be a good solution.

Frank






More information about the Python-list mailing list