0
我想從我的字典VALUES中獲取值。我的程序創建可能的職位組合並獲得最後的職位。然後,我想獲得價值。除了表示.get_value方法之外,一切運行良好。當我執行這個代碼,我得到:從OOP字典中獲取值:「AttributeError」
AttributeError的:「組合」對象有沒有屬性「的get_value」
理論上它應該很容易,但我相信新的面向對象,我看不出這裏有什麼問題。
X = ['A','B','C']
Y = ['1','2','3']
VALUES = {'A':10, 'B': 50, 'C':-20}
class Combination:
def __init__(self,x,y):
if (x in X) and (y in Y):
self.x = x
self.y = y
else:
print "WRONG!!"
def __repr__ (self):
return self.x+self.y
def get_x(self):
return self.x
def get_y(self):
return self.y
class Position:
def __init__(self):
self.xy = []
for i in X:
for j in Y:
self.xy.append(Combination(i,j))
def choose_last(self):
return self.xy.pop()
def __str__(self):
return "List contains: " + str(self.xy)
class Operation1:
def __init__(self):
self.operation1 = []
def __str__(self):
s = str(self.operation1)
return s
def get_value(self):
V = VALUES.get(self)
return V
pos = Position()
print pos
last_item = pos.choose_last()
print "Last item:", last_item, pos
last_value = last_item.get_value() # <---- Here is a problem
如何獲取我的職位價值?值由X值確定 - 這是A,B或C.在字典中,我有一個字母數字值。
我可以以某種方式使用我的Operation1類獲取此值嗎?因爲以後我想更多地使用這個類? – Kwiaci