Pythonの変数名

Pythonでちょっとつまづいたのでメモ。

Javaとかだと

class sample{

    static int f(int x){
        return x*2;
    }

    public static void main(String[] args) {
        int f = 3;
        System.out.println(f(5));
    }

}

10と出力される。メソッド名と変数名がかぶっても良い。

しかし、Pythonだと、

def f(x):
    return x*2

f = 3
print(f(5))

というプログラムを実行すると、

Traceback (most recent call last):
  File "sample.py", line 5, in <module>
    print(f(5))
TypeError: 'int' object is not callable

となってしまう。よく考えてみると動的型づけを行っているので、当然であった。