码迷,mamicode.com
首页 > 编程语言 > 详细

python内置函数4-eval()

时间:2017-02-21 20:01:21      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:eval python

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()

标签:eval python

原文地址:http://hdlptz.blog.51cto.com/12553181/1899777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!