TypeError:'GamePlayer'对象不可订阅

时间:2013-06-12 20:35:38

标签: python python-3.x

当我在使用#<---标记的行处运行代码时出现以下错误:

TypeError: 'GamePlayer' object is not subscriptable

显然,不可订阅means I don't have a certain method implemented。我不知道这是如何适用于我的代码,或如何解决它。从顶部开始的第二个函数是发生错误的位置。

传递给该函数的值是:["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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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