키가 1-1 일 때 간단한 매핑 클래스를 만들려고합니다. key1:key2, key2:key1
. 클래스가 입력 한 값과 같은지 확인하려고 할 때 오류가 발생합니다. True
을 가져야하지만 False
이 계속 나타납니다.dictonary 클래스 "매핑"오류가 발생했습니다.
>>> m=Mapping()
>>> m[2]=3
>>> m
Mapping({2: 3, 3: 2})
>>> m==Mapping({2: 3, 3: 2})
False
내가 뭘 잘못하고 있는지 잘 모르겠다. 내 코드는 아래에있다.
class Mapping():
def __init__(self, dic={}):
self.Dict = dict(dic)
def __repr__(self):
return "Mapping({})".format(self.Dict)
def __getitem__(self, loc=0):
return self.Dict[loc]
def pop(self, popn=0):
popm = self.Dict[popn]
self.Dict.pop(popn, None)
self.Dict.pop(popm, None)
def __setitem__(self, x, y):
self.Dict[x]=y
self.Dict[y]=x
'__eq__'을 구현하면 ... –
기본 dict와 기능상의 차이점이 없습니다. '__eq__ '마법 방법을 정의하는 것이 도움이되지만, 왜 사전을 기반으로 사전을 다시 구현하려고합니까? –
@NikolayProkopyev 그의 dict는 자동으로 역 매핑을 제공합니다. – Max