making python 2.3 not complain about generator expressions?

Discussion in 'Python' started by Dan Christensen, Jul 13, 2004.

  1. I'd like to be able to do something like:

    if pythonversion >= 2.4:
    g = (x for x in [1,2,3])
    else:
    g = iter([1,2,3])

    But even though the first branch doesn't execute in python 2.3, the
    parser still complains.

    I can think of some tricks using strings and eval. Is there anything
    more elegant?

    (By the way, what's the canonical way for a script to find out which
    version of python it is running in?)

    Dan
     
    Dan Christensen, Jul 13, 2004
    #1
    1. Advertising

  2. "Dan Christensen" <> wrote in message
    news:...
    > I'd like to be able to do something like:
    >
    > if pythonversion >= 2.4:
    > g = (x for x in [1,2,3])
    > else:
    > g = iter([1,2,3])
    >
    > But even though the first branch doesn't execute in python 2.3, the
    > parser still complains.


    How about

    if pythonversion >= 2.4:
    import newcode as code
    else:
    import oldcode as code
     
    Andrew Koenig, Jul 13, 2004
    #2
    1. Advertising

  3. On Mon, Jul 12, 2004 at 11:35:53PM -0400, Dan Christensen wrote:
    [...]
    >
    > (By the way, what's the canonical way for a script to find out which
    > version of python it is running in?)


    See sys.version, sys.version_info and sys.hexversion.

    -Andrew.
     
    Andrew Bennetts, Jul 13, 2004
    #3
  4. Dan Christensen

    Peter Hansen Guest

    Dan Christensen wrote:

    > (By the way, what's the canonical way for a script to find out which
    > version of python it is running in?)


    Expanding on Andrew Bennetts' answer, you typically do things like
    this:

    if sys.version_info[:2] >= (2, 2):
    print 'has 2.2 features in place'
    elif sys.version_info[:3] == (1, 5, 2):
    print 'precisely version 1.5.2'

    etc...

    That is, use slices of version_info as desired, comparing with
    constant tuples.

    -Peter
     
    Peter Hansen, Jul 13, 2004
    #4
    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. Emre  Sevinc
    Replies:
    1
    Views:
    472
  2. saneman
    Replies:
    2
    Views:
    336
    Thomas Tutone
    Oct 23, 2007
  3. Adam Akhtar
    Replies:
    9
    Views:
    174
    Seebs
    Jan 27, 2010
  4. Steven D'Aprano
    Replies:
    26
    Views:
    326
    Joshua Landau
    Oct 11, 2013
  5. Cameron Simpson
    Replies:
    0
    Views:
    143
    Cameron Simpson
    Oct 10, 2013
Loading...

Share This Page