TypeError:“GamePlayer”对象不是subscriptab

2024-06-28 20:46:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: keyselfdatadatetimereturnvaluedefline
2条回答

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

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

不过,我还是建议不要这样做。把它命名为p.fromX有什么不对?您的get_variableset_variable函数只是重新实现Python的基本特性。在

相关问题 更多 >