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.
パラダイムとは何か?
- プログラミングパラダイムとは、プログラムを組織するスタイルのことです。
- 同じ問題でも異なるスタイルで解決できます。
- Pythonでは、1つのプログラム内で複数のパラダイムを混ぜることができます。
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) のコードは、プログラムの状態を変更する一連のステップのリストです。
- 「どうやってやるか」を、ループや代入などによって段階的に説明します。
- プロシージャルスタイルでは、それらのステップを再利用可能な**手順(procedures)**や関数にまとめます。
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.
オブジェクト指向
- オブジェクト指向のコードは、データとそれに作用するメソッドをオブジェクトに束ねます。
- 問題を相互に通信し合うクラス群としてモデル化します。
- 各クラスが独立した部分であるため、大規模なプログラムに適しています。
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のリストcsi comprehension。
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.
低レベル
- 低レベルのコードは機械に近いものであり、アセンブリ言語やマシンコードです。
- 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)を記述し、単一のリストcsi comprehension [... 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. · 実行ボタンをクリックして出力を確認してください。