Life-time of temporary variables in list comprehensions

Discussion in 'Python' started by beginner, Oct 23, 2007.

  1. beginner

    beginner Guest

    Hi All,

    If I have a list comprehension:

    ab=["A","B"]
    c = "ABC"
    [1.0 if c=='A' else c='B' for c in ab]
    print c

    >>"B"


    My test shows that if c is not defined before the list comprehension,
    it will be created in the list comprehension; if it is defined before
    the list comprehension, the value will be overwritten. In other words,
    temp variables are not local to list comprehensions.

    My question is why is this and is there any way to make c local to
    list comp?

    Thanks,
    Geoffrey
     
    beginner, Oct 23, 2007
    #1
    1. Advertising

  2. On Tue, 2007-10-23 at 17:02 +0000, beginner wrote:
    > Hi All,
    >
    > If I have a list comprehension:
    >
    > ab=["A","B"]
    > c = "ABC"
    > [1.0 if c=='A' else c='B' for c in ab]


    "c='B'" is invalid syntax. Maybe you mean "c=='B'". That doesn't make
    much sense, but at least it's correct syntax.

    > print c
    >
    > >>"B"

    >
    > My test shows that if c is not defined before the list comprehension,
    > it will be created in the list comprehension; if it is defined before
    > the list comprehension, the value will be overwritten. In other words,
    > temp variables are not local to list comprehensions.
    >
    > My question is why is this and is there any way to make c local to
    > list comp?


    The only way to keep c from being overwritten currently is to avoid
    using a list comprehension. Generator expressions don't "leak" their
    iteration variable to the outside, so you can write this instead:

    list(1.0 if c=='A' else c=='B' for c in ab)

    HTH,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, Oct 23, 2007
    #2
    1. Advertising

  3. beginner schrieb:
    > Hi All,
    >
    > If I have a list comprehension:
    >
    > ab=["A","B"]
    > c = "ABC"
    > [1.0 if c=='A' else c='B' for c in ab]
    > print c
    >
    >>> "B"

    >
    > My test shows that if c is not defined before the list comprehension,
    > it will be created in the list comprehension; if it is defined before
    > the list comprehension, the value will be overwritten. In other words,
    > temp variables are not local to list comprehensions.
    >
    > My question is why is this and is there any way to make c local to
    > list comp?



    Unfortunately, not as such. They do leak their variable names.

    But what you can do ist to create generator expressions (which don't
    leak) and put a list around them:

    list(1.0 if c == 'A' else c="B" for c in ab)

    See also

    http://www.python.org/dev/peps/pep-0289/

    Diez
     
    Diez B. Roggisch, Oct 23, 2007
    #3
  4. On Tue, 23 Oct 2007 17:02:48 +0000, beginner wrote:

    > My test shows that if c is not defined before the list comprehension, it
    > will be created in the list comprehension; if it is defined before the
    > list comprehension, the value will be overwritten. In other words, temp
    > variables are not local to list comprehensions.


    That's right.

    > My question is why is this and is there any way to make c local to list
    > comp?


    It was a design decision that is now regretted. There is no way for you
    to make it local to the list comp except to wait for a new version of
    Python that behaves in the way you want.

    Or you can look at generator expressions, which are written just like
    list comprehensions except with round brackets (). They are not quite the
    same thing, but you can often use one in place of the other.


    --
    Steven.
     
    Steven D'Aprano, Oct 23, 2007
    #4
  5. beginner

    beginner Guest

    On Oct 23, 12:02 pm, beginner <> wrote:
    > Hi All,
    >
    > If I have a list comprehension:
    >
    > ab=["A","B"]
    > c = "ABC"
    > [1.0 if c=='A' else c='B' for c in ab]
    > print c
    >
    > >>"B"

    >
    > My test shows that if c is not defined before the list comprehension,
    > it will be created in the list comprehension; if it is defined before
    > the list comprehension, the value will be overwritten. In other words,
    > temp variables are not local to list comprehensions.
    >
    > My question is why is this and is there any way to make c local to
    > list comp?
    >
    > Thanks,
    > Geoffrey


    I see.

    Thanks for everyone's help!
     
    beginner, Oct 23, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. seguso
    Replies:
    9
    Views:
    395
    seguso
    Dec 22, 2004
  2. Dave Kuhlman

    Re: Style in list comprehensions

    Dave Kuhlman, Aug 15, 2003, in forum: Python
    Replies:
    1
    Views:
    337
    Alex Martelli
    Aug 16, 2003
  3. Frank Millman

    Simple db using list comprehensions

    Frank Millman, Apr 5, 2004, in forum: Python
    Replies:
    1
    Views:
    293
    Paddy McCarthy
    Apr 16, 2004
  4. Steven Bethard
    Replies:
    7
    Views:
    404
    Rocco Moretti
    Jan 20, 2006
  5. Replies:
    7
    Views:
    3,261
    James Kanze
    Feb 12, 2008
Loading...

Share This Page