1. 程式人生 > 其它 >github上的drqv2中的main code 程式碼報錯:*** TypeError: getattr(): attribute name must be string 。

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)

經驗證,程式碼無誤。