github上的drqv2中的main code 代码报错:*** TypeError: getattr(): attribute name must be string 。

class ExtendedTimeStep(NamedTuple):
# step_type: Any
# reward: Any
# discount: Any
# observation: Any
# action: Any

def __init__(self, step_type, reward, discount, observation, action):
self.step_type = step_type
self.reward = reward
self.discount = discount
self.observation = observation
self.action = action

def first(self):
return self.step_type == StepType.FIRST

def mid(self):
return self.step_type == StepType.MID

def last(self):
return self.step_type == StepType.LAST

def __getitem__(self, attr):
return getattr(self, attr)

以上代码报错:*** TypeError: getattr(): attribute name must be string 。
经检查,发现是该class继承的NamedTuple有问题,现去掉继承的实体,并且加上__init__,如下:

# class ExtendedTimeStep(NamedTuple):
class ExtendedTimeStep:
# step_type: Any
# reward: Any
# discount: Any
# observation: Any
# action: Any

def __init__(self, step_type, reward, discount, observation, action):
self.step_type = step_type
self.reward = reward
self.discount = discount
self.observation = observation
self.action = action

def first(self):
return self.step_type == StepType.FIRST

def mid(self):
return self.step_type == StepType.MID

def last(self):
return self.step_type == StepType.LAST

def __getitem__(self, attr):
return getattr(self, attr)

经验证,代码无误。





posted @ 2022-01-06 20:32  呦呦南山  阅读(538)  评论(0编辑  收藏  举报