[Python] build-in objectに独自のメソッドを追加する禁断の果実

1 · · Oct. 9, 2019, 1:14 a.m.
Summary
Pythonはクラスに後からメソッドが追加できる言語です。 class A: pass A().hello() # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # AttributeError: 'A' object has no attribute 'hello' def hello(self): print("hello") setattr(A,'hello',hello) A().hello() # hello しかし、それはあく...