10. Pythonの特徴1
インデントが制御構造をあらわす
10
if a > b:
c = a
else:
c = b
def abs(a, b):
c = a – b
if c < 0:
c = -c
return c
条件文
関数定義
if (a > b) {
c = a;
} else {
c = b;
}
int abs(int a, int b)
{
c = a – b;
if (c < 0) {
c = -c;
}
return c;
}
Python C
11. Pythonの特徴2
オブジェクトのインスタンス変数に直接アクセスできない。
11
class Foo():
def __init__(self, a):
"""コンストラクタ"""
self.x = a
def mul(self, a):
"""xをa倍する"""
self.x *= a
class 定義
呼び出し例
bar = Foo(3) # bar.x = 3
Bar.mul(5) # bar.x = 15
class Foo {
public:
int x;
Foo(int a) {
this->x = a;
}
void mul(int a) {
this->x *= a;
}
};
Python C++
Foo baa(3);
baa.mul(5);