1

当我在标有 a 的行上运行代码时,出现以下错误#<---

TypeError: 'GamePlayer' object is not subscriptable.

显然,不可下标意味着我没有实现某种方法。我不知道这如何适用于我的代码,或者如何修复它。从上数第二个函数是发生错误的地方。

传递给该函数的值是:(["b'direction:north", 'fromTime:2013-06-12 16:32:27.102400', 'fromY:0', 'fromX:0', "identity:1,'"]我不知道如何摆脱b'),但我确定它不会导致问题。

@staticmethod
def data_line_to_dict(data):
    decoded = dict()
    data_peices = str(data).split(', ')
    print(data_peices)
    for d in data_peices:
        key_val = d.split(':')
        print(key_val)
        decoded[key_val[0]] = key_val[1]
    return decoded

@staticmethod
def create_from_data_line(data): #The value of data is: ["b'direction:north", 'fromTime:2013-06-12 16:32:27.102400', 'fromY:0', 'fromX:0', "identity:1,'"]
    dictionary_data = GamePlayer.data_line_to_dict(data.strip())
    p = GamePlayer()
    p.set_variable('fromX', int(p['fromX'])) #<--- where error occurs
    p.set_variable('fromY', int(p['fromY']))
    date = datetime.datetime.strptime(p['fromTime'], '%Y-%m-%d %H:%M:%S')
    p.set_variable('fromTime', data)
    p.set_variable('identity', int(p['identity']))
    return p

def create_data_line(self):
    final = ""
    for k in self.values:
        v = self.values[k]
        final += str(k) + ":" + str(v)
        final += ", "
    return final

def set_variable(self, variable, value):
    self.values[variable] = value

def get_variable(self, variable):
    return self.values[variable]

def get_microseconds_in_direction(self):
    now = datetime.datetime.today()
    diff = now - self.get_value['fromDate']
    return diff.microseconds
4

2 回答 2

1

问题是您在( ) 上使用了[]运算符,但您没有实现它。我假设你打算这样做?GamePlayerp['fromX']p.get_variable("fromX")

我建议不要这样做。只是命名有什么问题p.fromX?您的get_variableset_variable函数只是重新实现 Python 的基本功能。

于 2013-06-12T20:39:24.383 回答
0

尝试p.values['fromX']p.get_variable('fromX')

于 2013-06-12T20:42:02.257 回答