eval() in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    eval() in python

    is it possible to eval a string like the following?

    m='''
    i0=[0,1]
    i1=[2,3]
    i2=[4,'a']

    h0=[]
    for j0 in i0:
    h1=[]
    for j1 in i1:
    h2=[]
    for j2 in i2:
    h2.append(f(j0, j1,j2))
    h1.append( h2[:] )
    h0.append( h1[:] )
    return h0'''

    perhaps i'm tired, but why can't i run:

    t='m=3'
    print eval(t)

    the doc seems to suggest that eval is only for expressions... it says
    uses exec for statements, but i don't seem to see a exec function?

    Xah
    xah@xahlee.org
    ∑ http://xahlee.org/

  • harold fellermann

    #2
    Re: eval() in python

    > the doc seems to suggest that eval is only for expressions... it says[color=blue]
    > uses exec for statements, but i don't seem to see a exec function?[/color]

    Python 2.4 (#1, Dec 30 2004, 08:00:10)
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> s="print 'hello Xah Lee :-)'"
    >>> exec(s)[/color][/color][/color]
    hello Xah Lee :-)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    - harold -

    --
    I wish there was a knob on the TV to turn up the intelligence. There's
    a
    knob called "brightness ", but it doesn't seem to work.
    -- Gallagher

    Comment

    • Peter Hansen

      #3
      Re: eval() in python

      Xah Lee wrote:[color=blue]
      > the doc seems to suggest that eval is only for expressions... it says
      > uses exec for statements, but i don't seem to see a exec function?[/color]

      Because it's a statement: http://docs.python.org/ref/exec.html#l2h-563

      Comment

      • Jeff Epler

        #4
        Re: eval() in python

        On Tue, Jun 21, 2005 at 08:13:47AM -0400, Peter Hansen wrote:[color=blue]
        > Xah Lee wrote:[color=green]
        > > the doc seems to suggest that eval is only for expressions... it says
        > > uses exec for statements, but i don't seem to see a exec function?[/color]
        >
        > Because it's a statement: http://docs.python.org/ref/exec.html#l2h-563[/color]

        but the documentation is sooooooo baaaaaaad that it makes babies cry and
        maybe spreads herpes too.

        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.6 (GNU/Linux)

        iD8DBQFCuAlUJd0 1MZaTXX0RAqtVAJ sE6RPF5wIs5+g4O qxH4ar76ZSFDgCe O6bd
        13SXkT2AdxaPxk9 Ea/D1wj0=
        =8V9f
        -----END PGP SIGNATURE-----

        Comment

        • Benji York

          #5
          Re: eval() in python

          harold fellermann wrote:[color=blue][color=green][color=darkred]
          > >>> s="print 'hello Xah Lee :-)'"
          > >>> exec(s)[/color][/color]
          > hello Xah Lee :-)[/color]

          Note that because "exec" is a statement, the parentheses above are
          superfluous.
          --
          Benji York

          Comment

          • Martin Blume

            #6
            Re: eval() in python

            "Xah Lee" schrieb[color=blue]
            >
            > perhaps i'm tired, but why can't i run:
            >
            > t='m=3'
            > print eval(t)
            >[/color]
            Perhaps you didn't read the documentation? :-)
            Perhaps you didn't try hard enough?


            C:\WINNT>c:\pro gramme\python\p ython
            Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)]
            on win32
            Type "help", "copyright" , "credits" or "license" for more
            information.[color=blue][color=green][color=darkred]
            >>> t='m=3'
            >>> m[/color][/color][/color]
            Traceback (most recent call last):
            File "<stdin>", line 1, in ?
            NameError: name 'm' is not defined[color=blue][color=green][color=darkred]
            >>> exec(t)
            >>> m[/color][/color][/color]
            3[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            HTH
            Martin


            Comment

            Working...