Programming paradigms · 编程范式
What is a paradigm?
- A programming paradigm is a style of organising a program.
- The same problem can be solved in different styles.
- Python lets you mix several paradigms in one program.
什么是范式?
- 编程范式(programming paradigm)是组织程序的一种风格。
- 同一个问题可以用不同的风格来解决。
- Python 允许你在一个程序里混用多种范式。
Imperative and procedural
- Imperative code is a list of steps that change the program's state.
- It says how to do something, step by step (loops, assignments).
- Procedural style groups those steps into reusable procedures and functions.
命令式与过程式
- 命令式(imperative)代码是一串改变程序状态的步骤。
- 它一步一步地说明如何做某件事(循环、赋值)。
- 过程式(procedural)风格把这些步骤组织成可复用的过程和函数。
def total(nums):
result = 0
for n in nums:
result = result + n
return result
print(total([2, 4, 6]))
Object-oriented
- Object-oriented code bundles data and the methods that act on it into objects.
- You model the problem as a set of classes that talk to each other.
- It suits large programs because each class is a self-contained part.
面向对象
- 面向对象(object-oriented)代码把数据和操作它的方法打包进对象里。
- 你把问题建模成一组相互协作的类。
- 它适合大型程序,因为每个类都是一个自成一体的部分。
class Counter:
def __init__(self):
self.count = 0
def add(self):
self.count = self.count + 1
c = Counter()
c.add()
c.add()
print(c.count)
Declarative
- Declarative code says what you want, not how to compute it.
- The language or library works out the steps for you.
- Examples: a database query (SQL), or a Python list comprehension.
声明式
- 声明式(declarative)代码说明你想要什么,而不是如何计算它。
- 由语言或库替你算出具体步骤。
- 例子:数据库查询(SQL),或 Python 的列表推导式。
nums = [1, 2, 3, 4, 5]
print(sum(nums)) # say WHAT: the total
print([n * n for n in nums]) # a list comprehension
Low-level
- Low-level code is close to the machine: assembly and machine code.
- The CPU runs it directly, so it is fast but hard to write.
- High-level languages like Python are translated down to it before running.
低级语言
- 低级(low-level)代码贴近机器:汇编语言和机器码。
- CPU 直接运行它,所以速度快,但很难编写。
- 像 Python 这样的高级语言,在运行前会被翻译成它。
Common mistakes
- Procedural is steps; object-oriented is objects with data and methods; declarative describes the result.
- Match the paradigm to the problem.
常见错误
- 过程式是步骤;面向对象是带数据和方法的对象;声明式描述结果。
- 让范式匹配问题。
Now you try
- Each task solves a small problem in a different paradigm.
- Press Check answer to test your code.
现在轮到你
- 每个任务都用一种不同的范式来解决一个小问题。
- 按检查答案来测试你的代码。
Procedural style. Write sum_list(nums) that uses a loop and an accumulator to add up the list and return · 回报 the total. · 过程式风格。 编写 sum_list(nums),用一个循环和累加器把列表加起来,并返回总和。
Click Run to see the output here. · 点击“运行”查看此处输出。
Declarative style. Write evens(nums) that returns · 返回值 a list of the even numbers, using a single list comprehension [... for ... if ...]. · 声明式风格。 编写 evens(nums),用一个列表推导式 [... for ... if ...],返回所有偶数组成的列表。
Click Run to see the output here. · 点击“运行”查看此处输出。
Object-oriented style. Write a class Bag whose __init__(self, items) stores a list, and whose method total(self) returns · 返回值 the sum of those items. · 面向对象风格。 编写一个类 Bag,它的 __init__(self, items) 存下一个列表,方法 total(self) 返回这些元素的总和。
Click Run to see the output here. · 点击“运行”查看此处输出。