python命令式编程_命令式编程和指令式编程的区别

本文探讨了命令式编程与符号式编程的区别,通过具体的例子展示了两种编程方式的工作原理。文章指出,虽然命令式编程易于理解和调试,但在效率方面可能不如符号式编程。后者在编译阶段可以进行更多的优化,并能生成独立于Python的可移植程序。
摘要由CSDN通过智能技术生成

Imperative programming makes use of programming statements to change a program’s state. Consider the following example of simple imperative programming code.

def add(a, b):

return a + b

def fancy_func(a, b, c, d):

e = add(a, b)

f = add(c, d)

g = add(e, f)

return g

fancy_func(1, 2, 3, 4)

As expected, Python will perform an addition when running the statement e = add(a, b), and will store the result as the variable e, thereby changing the program’s state. The next two statements f = add(c, d) and g = add(e, f) will similarly perform additions and store the results as variables.

Although imperative programming is convenient, it may be inefficient. On the one hand, even if the add function is repeatedly called throughout the fancy_func function, Python will execute the three function calling statements individually, one after the other. On the other hand, we need to save the variable values of e and f until all the statements in fancy_func have been executed. This is because we do not know whether the variables e and f will be used by other parts of the program after the statements e = add(a, b) and f = add(c, d) have been executed.

Contrary to imperative programming, symbolic programming is usually performed after the computational process has been fully defined. Symbolic programming is used by multiple deep learning frameworks, including Theano and TensorFlow. The process of symbolic programming generally requires the following three steps:

Define the computation process.

Compile the computation process into an executable program.

Provide the required inputs and call on the compiled program for execution.

In the example below, we utilize symbolic programming to re-implement the imperative programming code provided at the beginning of this section.

def add_str():

return '''

def add(a, b):

return a + b

'''

def fancy_func_str():

return '''

def fancy_func(a, b, c, d):

e = add(a, b)

f = add(c, d)

g = add(e, f)

return g

'''

def evoke_str():

return add_str() + fancy_func_str() + '''

print(fancy_func(1, 2, 3, 4))

'''

prog = evoke_str()

print(prog)

y = compile(prog, '', 'exec')

exec(y)

The three functions defined above will only return the results of the computation process as a string. Finally, the complete computation process is compiled and run using the compile function. This leaves more room to optimize computation, since the system is able to view the entire program during its compilation. For example, during compilation, the program can be rewritten as print((1 + 2) + (3 + 4)) or even directly rewritten as print(10). Apart from reducing the amount of function calls, this process also saves memory.

A comparison of these two programming methods shows that

imperative programming is easier. When imperative programming is used in Python, the majority of the code is straightforward and easy to write. At the same time, it is easier to debug imperative programming code. This is because it is easier to obtain and print all relevant intermediate variable values, or make use of Python’s built-in debugging tools.

Symbolic programming is more efficient and easier to port. Symbolic programming makes it easier to better optimize the system during compilation, while also having the ability to port the program into a format independent of Python. This allows the program to be run in a non-Python environment, thus avoiding any potential performance issues related to the Python interpreter.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值