"eval" function
Gerrit Holl
gerrit at nl.linux.org
Sun Jun 22 16:20:43 EDT 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
True
There is an 'eval' function in Python, but with a different
behaviour:
6 >>> help(eval)
Help on built-in function eval:
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)
True
Or:
10 >>> mystr = 'open("/dev/zero", "r")'
11 >>> eval(mystr).read(50)
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
See also:
http://www.python.org/dev/doc/devel/lib/staticmethod.html#l2h-19
yours,
Gerrit.
--
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
loss.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/
More information about the Python-list
mailing list