Python动态执行语句 Executable Object Statement and Builtin Functions

Executable Object Statements and Built-in Functions

Built-in Function or Statement Description

callable(obj)  Returns true if obj is callable and False otherwise
compile(string, file, type)  Creates a code object from string of type type; file
is where the code originates from (usually set to "")
eval(obj, globals=globals(), locals=locals())  Evaluates obj, which is either an expression compiled
into a code object or a string expression; global and/
or local namespace may also be provided
exec obj  Executes obj,a single Python statement or set of
statements, either in code object or string format;
obj may also be a file object (opened to a valid Python script)
input(prompt='')  Equivalent to eval(raw_input(prompt=''))

14.3.1. callable()

callable() is a Boolean function that determines if an object type can be invoked via the function
operator ( ( ) ). It returns true if the object is callable and False otherwise (1 and 0, respectively, for
Python 2.2 and earlier). Here are some sample objects and what callable returns for each type:
>>> callable(dir) # built-in function
True
>>> callable(1) # integer
False
>>> def foo(): pass
...
>>> callable(foo) # user-defined function
True
>>> callable('bar') # string
False
>>> class C(object): pass
...
>>> callable(C) # class
True


Section 14.3. Executable Object Statements and Built-in Functions
14.3.2. compile()
compile() is a function that allows the programmer to generate a code object on the fly, that is, during
runtime. These objects can then be executed or evaluated using the exec statement or eval() BIF. It is
important to bring up the point that both exec and eval() can take string representations of Python code
to execute. When executing code given as strings, the process of byte-compiling such code must occur
every time. The compile() function provides a one-time byte-code compilation of code so that the
precompile does not have to take place with each invocation. Naturally, this is an advantage only if the
same pieces of code are executed more than once. In these cases, it is definitely better to precompile
the code.
All three arguments to compile() are required, with the first being a string representing the Python code
to compile. The second string, although required, is usually set to the empty string. This parameter
represents the file name (as a string) where this code object is located or can be found. Normal usage is
for compile() to generate a code object from a dynamically generated string of Python codecode that
obviously does not originate from an existing file.
The last argument is a string indicating the code object type. There are three possible values:
'eval' Evaluatable expression [to be used with eval()]
'single' Single executable statement [to be used with exec]
'exec' Group of executable statements [to be used with exec]
Evaluatable Expression
>>> eval_code = compile('100 + 200', '', 'eval')
>>> eval(eval_code)
300
Single Executable Statement
>>> single_code = compile('print"Hello world!"', '', 'single')
>>> single_code
<code object ? at 120998, file "", line 0>
>>> exec single_code
Hello world!
Group of Executable Statements
>>> exec_code = compile("""
... req = input('Count how many numbers? ')
... for eachNum in range(req):
... print eachNum
... """, '', 'exec')
>>> exec exec_code
Count how many numbers? 6
0


In the final example, we see input() for the first time. Since the beginning, we have been reading input
from the user using raw_input(). The input() BIF is a shortcut function that we will discuss later in this
chapter. We just wanted to tease you with a sneak preview.

One clear example is when the user inputs a list. raw_input() returns the string representation of a list,
while input() returns the actual list:
>>> aString = raw_input('Enter a list: ')
Enter a list: [ 123, 'xyz', 45.67 ]
>>> aString
"[ 123, 'xyz', 45.67 ]"
>>> type(aString)
<type 'str'>


The above was performed with raw_input(). As you can see, everything is a string. Now let us see what
happens when we use input() instead:
>>> aList = input('Enter a list: ')
Enter a list: [ 123, 'xyz', 45.67 ]
>>> aList
[123, 'xyz', 45.67]
>>> type(aList)
<type 'list'>

原文地址:https://www.cnblogs.com/wucg/p/2339744.html