"eval" function

Gerrit Holl gerrit at nl.linux.org
Sun Jun 22 22:20:43 CEST 2003

Hi Eirik,

Eirik wrote:
> How can I do what I would do like this:
> set a 12
> set b 144
> eval a*a == b
> in Tcl?

I'm not sure what this means in Tcl, but you can check for
equality using the '==' operator. You don't need to use
'eval' for that. You can simply do "a*a==b":

  3 >>> a=12
  4 >>> b=144
  5 >>> a*a==b

There is an 'eval' function in Python, but with a different

  6 >>> help(eval)
Help on built-in function eval:

    eval(source[, globals[, locals]]) -> value

    Evaluate the source in the context of globals and locals.
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals and locals are dictionaries, defaulting to the current
    globals and locals.  If only globals is given, locals defaults to it.

  8 >>> mystr = 'a*a==b'
  9 >>> eval(mystr)

 10 >>> mystr = 'open("/dev/zero", "r")'
 11 >>> eval(mystr).read(50)

See also:



265. If a herdsman, to whose care cattle or sheep have been entrusted,
be guilty of fraud and make false returns of the natural increase, or sell
them for money, then shall he be convicted and pay the owner ten times the
        -- 1780 BC, Hammurabi, Code of Law
Asperger Syndroom - een persoonlijke benadering:
Het zijn tijden om je zelf met politiek te bemoeien:

More information about the Python-list mailing list