(a==b) ? 'Yes' : 'No'

Tim Chase python.list at tim.thechases.com
Wed Mar 31 04:25:13 CEST 2010


John Bokma wrote:

> And maybe you're right, the Python one could've been written:
> 
> if list is None:
>    list = []
> 
> which looks, now, also more readable to me as well.

Though there's a slight difference[1], I'd usually use

   lst = lst or []

for your particular initialization use case.

-tkc

[1]
Difference being

   >>> lst = []
   >>> other = lst
   >>> if lst is None: # your code
   ...     lst = []
   ...
   >>> other.append(42)
   >>> lst, other
   ([42], [42])

   >>> lst = []
   >>> other = lst
   >>> lst = lst or [] # my proposal
   >>> other.append(42)
   >>> lst, other
   ([], [42])




More information about the Python-list mailing list