python动态执行语句_Python动态执行语句 Executable Object Statement and Built-in 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

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

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值