Evaluates any string as a Python expression:
Eval () Method:
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 must be a dictionary and locals can is any mapping,
Defaulting to the current globals and locals.
If only globals are given, locals defaults to it.
Usage:
>>> eval (' 1+1==2 ') true>>> eval (' 1+1==3 ') false>>> eval (' 9567+1085 = 10652 ') true>> > eval (' A ' + ' B ' ') ' AB ' >>> eval (' "MARK". Translate ({65:79}) ') ' Mork ' >>> eval (' "AAAAA". Count ("a") ‘)
>>> eval ("X*5", {}, {}) Traceback (most recent call last): File "<pyshell#120>", line 1, in <MODULE&G t; Eval ("X*5", {}, {}) File "<string>", line 1, <module>nameerror:name ' x ' are not defined>>> eval ("X*5", {"X": x},{}) 25>>> import math>>> eval ("Math.sqrt (x)", {"X": x},{}) Traceback (most recent call Last): file ' <pyshell#123> ', line 1, in <module> eval ("Math.sqrt (x)", {"X": x},{}) file "< String> ", line 1, in <module>nameerror:name ' math ' are not defined
Note:
The second and third parameters passed to the eval () function serve as the role of the global and local namespaces for evaluation expressions
Eval () is unsafe, and for safe evaluation of untrusted expressions, you need to define a global namespace dictionary that maps "__builtins__" to none. Internally, the "built-in" function is contained within a pseudo-module called "__builtins__".
Re.findall ()--Returns all letters in a string
Set ()--Returns all the different letters in the string
Python Learning note 7-Advanced iterators