首页 > 代码库 > python内置函数4-eval()

python内置函数4-eval()

Help on built-in function eval in module __builtin__:


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 must be a dictionary and locals can be any mapping,

    defaulting to the current globals and locals.

    If only globals is given, locals defaults to it.

eval(expression[, globals[, locals]])

The arguments are a Unicode or Latin-1 encoded string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object.


Changed in version 2.4: formerly locals was required to be a dictionary.


The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is present and lacks ‘__builtins__’, the current globals are copied into globals before expression is parsed. This means that expression normally has full access to the standard __builtin__ module and restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval() is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:


>>>

>>> x = 1

>>> print eval(‘x+1‘)

2

This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case pass a code object instead of a string. If the code object has been compiled with ‘exec‘ as the mode argument, eval()‘s return value will be None.


Hints: dynamic execution of statements is supported by the exec statement. Execution of statements from a file is supported by the execfile() function. The globals() and locals() functions returns the current global and local dictionary, respectively, which may be useful to pass around for use by eval() or execfile().


See ast.literal_eval() for a function that can safely evaluate strings with expressions containing only literals.


中文说明:将字符串str当成有效的表达式来求值并返回计算结果。

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

参数:

source:一个Python表达式或函数compile()返回的代码对象

globals:可选。必须是dictionary

locals:可选。任意map对象


>>> a="[[1,2],[3,4],[5,6],[7,8],[9,0]]"

>>> type(a)

<type ‘str‘>

>>> b=eval(a)

>>> print b

[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]

>>> type(b)

<type ‘list‘>

>>> a="{1: ‘a‘, 2: ‘b‘}"

>>> type(a)

<type ‘str‘>

>>> b=eval(a)

>>> print b

{1: ‘a‘, 2: ‘b‘}

>>> type(b)

<type ‘dict‘>

>>> a="([1,2],[3,4],[5,6],[7,8],[9,0])"

>>> type(a)

<type ‘str‘>

>>> b=eval(a)

>>> print b

([1, 2], [3, 4], [5, 6], [7, 8], [9, 0])

>>> type(b)

<type ‘tuple‘>


本文出自 “大云技术” 博客,请务必保留此出处http://hdlptz.blog.51cto.com/12553181/1899777

python内置函数4-eval()