如何創建加在3.5命名元組自定義超載?我知道3.6中有一些新的語法,你能在3.5中做到嗎?我希望它也能通過mypy檢查。我如何重載'+'上NamedTuple
from typing import NamedTuple
Point = NamedTuple('Point',[('x',int),
('y',int)])
def joinPoints(a: Point, b:Point) -> Point:
return Point(x = a.x+b.x,y=a.y+b.y)
q = Point(1,2)
r = Point(3,4)
s= joinPoints(q,r)
t = q+r #HOW DO I MAKE THIS GO?
#s should equal t
你指的是什麼新的語法?你需要繼承namedtuple和覆蓋'__add__' –
你必須創建一個子類來定義'__add__' – theWanderer4865
我不想一個子類,我想特質/基於類型的調度,如函數式語言:/ – Carbon