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)
经验证,代码无误。