Skip to content

Programming paradigms

Python for A-Level CS Lesson 12 2:45 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
A paradigm is a style of organising a program — not a language, a style. 范式是一种组织程序的风格——不是一门语言,是一种风格。
The same problem can be written in any of them. 同一个问题可以用其中任何一种写出来。
Procedural is a list of steps, grouped into procedures. 过程式是一串步骤,被归拢进各个过程里。
Object-oriented bundles the data together with the methods that act on it. 面向对象把数据和作用于它的方法捆在一起。
Declarative describes what you want and leaves the steps to the library. 声明式描述你要什么,把步骤留给库去解决。
And low-level is the machine's own instructions. 而低级语言就是机器自己的指令。
Python lets you mix the first three freely, and this lesson does. Python 允许你自由混用前三种,这一课就会这么做。
Here is adding up a list, written procedurally. 这是用过程式写的"把一个列表加起来"。
It is imperative code: it says how, one instruction at a time, and it works by changing state. 它是命令式代码:它说的是"怎么做",一次一条指令, 而它的工作方式是改变状态。
Watch the state — there is one variable that changes, and the whole answer lives in it. 看那个状态——只有一个变量在变,整个答案都住在它里面。
Two, then six, then twelve. 2,然后 6,然后 12。
Wrapping those steps in a function called total is the procedural part: a reusable named procedure. 把这些步骤包进一个叫 total 的函数里,就是"过程式"的那一半: 一个可复用的、有名字的过程。
The same counting, object-oriented. 同样的计数,用面向对象来写。
The number lives on the object, and the only thing that knows how to change it is a method of the same class. 那个数字住在对象上, 而唯一知道怎么改动它的,是同一个类的一个方法。
Call add twice and the count goes to two. 调用 add 两次,count 就变成 2。
The gain is not fewer lines — it is that count can change only through add, so every rule about counting is in one place. 好处不是代码更短—— 而是 count 只能通过 add 改变, 所以关于计数的每一条规则都集中在一个地方。
That is why large programs are built this way. 这就是大型程序这样构建的原因。
Now the same total again, declaratively. 现在再一次求同样的总和,这回用声明式。
On the left, every step spelled out. 左边,每一步都写了出来。
On the right, one word instead — sum of nums. 右边,只用一个词代替——sum(nums)。
You did not say to start at nought, or to loop, or in what order; you said what you wanted and something else worked out how. 你没有说从 0 开始,没有说要循环,也没有说按什么顺序; 你说的是你要什么,由别的东西去想怎么做。
A list comprehension is the same move, and so is a database query, which is why SQL is the standard example. 列表推导式是同一个动作,数据库查询也是, 这就是为什么 SQL 是那个标准例子。
And the fourth one sits underneath all of them. 而第四种,坐在所有这些之下。
Whichever style you wrote, it is translated down: to assembly, which names the machine's instructions, and then to machine code, which is the numbers the processor actually executes. 不管你用哪种风格写,它都会被向下翻译: 翻译成汇编——汇编给机器指令起了名字—— 再翻译成机器码,那才是处理器真正执行的数字。
Writing at that level is fast but painful, and it is why high-level languages exist at all. 在那个层次上写代码很快,但很痛苦, 这也正是高级语言存在的全部理由。
Four things to take with you. 带走四点。
One: procedural code is steps, grouped into procedures. 第一:过程式代码是一串步骤,被归拢进过程里。
Two: object-oriented code bundles data with its methods. 第二:面向对象代码把数据和它的方法捆在一起。
Three: declarative code describes the result, not the steps. 第三:声明式代码描述结果,而不是步骤。
Four: low-level code is what the CPU actually runs. 第四:低级代码才是 CPU 真正运行的东西。
The three tasks are the same small problem in three styles — do them in order. 那三道题是同一个小问题的三种风格——按顺序做完它们。

Log in or create account

IGCSE, A-Level & AP