[Python-ideas] ('blue', 'red', 'orange' if something, 'green')

Fred Drake fdrake at acm.org
Fri Apr 22 13:39:54 CEST 2011


On Fri, Apr 22, 2011 at 6:57 AM, cool-RR <cool-rr at cool-rr.com> wrote:
> I used a tuple in this case because it's something that I wanted to stay
> immutable.

This is a perfect reason to use a tuple in my book.  A common and
handy approach with objects persisted in databases is to have the
persistent object track mutations, and make the attribute values
themselves immutable.  (I'm thinking ZODB in particular, but other
persistence implementations support similar models.)

Something I've often wanted to do is to construct a sequence (either
list or tuple) based on both constant and variable portions:

    >>> source = [...some list of things...]
    >>> result = [a, b, *(x for x in source if condition)]


  -Fred

-- 
Fred L. Drake, Jr.    <fdrake at acm.org>
"Give me the luxuries of life and I will willingly do without the necessities."
   --Frank Lloyd Wright



More information about the Python-ideas mailing list