Python built-in function 4-eval ()

Source: Internet
Author: User

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 is any mapping,

Defaulting to the current globals and locals.

If only globals are given, locals defaults to it.

Eval (expression[, globals[, locals])

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


Changed in version 2.4:formerly Locals is required to bes 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 was copied into globals before Expressi On is parsed. This means that expression normally have full access to the standard __builtin__ module and restricted environments is pro Pagated. If the locals dictionary is omitted it defaults to the Globals dictionary. If both dictionaries is omitted, the expression was executed in the environment where eval () is called. The return value is the result of the evaluated expression. Syntax errors is reported as exceptions. Example:


>>>

>>> x = 1

>>> Print eval (' x+1 ')

2

This function can also is 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 a been compiled with ' exec ' as the Mode argument, eval () ' s return value would be None.


Hints:dynamic execution of statements is supported by the EXEC statement. Execution of statements from a file are supported by the execfile () function. The Globals () and locals () functions returns the current global and local dictionary, respectively, which could useful to Pass around for use by eval () or execfile ().


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


英文说明: Evaluates the string str as a valid expression and returns the result of the calculation.

Syntax: eval (source[, globals[, locals]), value

Parameters:

Source: The code object returned by a Python expression or function compile ()

Globals: Optional. It must be dictionary.

Locals: Optional. Arbitrary map Object


>>> 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 ' >


This article is from the "Big Cloud Technology" blog, please be sure to keep this source http://hdlptz.blog.51cto.com/12553181/1899777

Python built-in function 4-eval ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.