"마법 기능"스택 추적 속성 또는 Python3 내 클래스 (__add__
, __sub__
등)Python3 내가 소위 "마법"을 많이 재정의하고있는 상황에서 자신을 찾을 수
의 경우의 기능 이 모든, 나는 동일한 코드 두 줄을 구현 :이 라인이하는 일의
arg1 = self.decimal if isinstance(self, Roman) else self
arg2 = other.decimal if isinstance(other, Roman) else other
세부 사항은 중요하지 않습니다, 그러나, 내 코드의 중복 산만이다. REPL에서 호출되는 중간과 다른 "마법"기능이 있습니까? 예를 들어
이 같은의 스택 트레이스와
>> Class(9) + Class(3)
... (somewhere in python module)
... def __magicFunction__(self, rhs):
... arg1 = self.decimal if isinstance(self, Roman) else self
... arg2 = other.decimal if isinstance(other, Roman) else other
...
... THEN
...
... def __add__(self, rhs):
... return arg1 + arg2
...
12
:
Traceback (most recent call last):
File "< stdin>", line 1, in < module>
File "/home/module.py", line 105, in ```__magicFunction__```
File "/home/module.py", line 110, in ```__gt__```
나는이 말이 희망 ...
어구에 대한 또 다른 방법은 매직 함수가 호출 될 때마다 호출되는 마술 함수입니까? – Jeremy
IPython 마법은 완전히 다른 짐승입니다. http://stackoverflow.com/questions/15196360/python-overloading-several-operators-at-once – filmor