初学者-python中自定义类的已有self定义,后面使用出现object has no attribute问题

修改别人python代码,会遇到在类的init中已定义self,但后面使用还是找不到定义的self.*,其中一个原因是init中self定义顺序的问题,比如:如下为正确的

class LSTM(object):

def __init__(self, a, b, c, d, e, f):

        super(LSTM, self).__init__()
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.e = e
        self.f = f
        self.add_in_layer()
        self.add_lstm_layer()
        self.add_out_layer()

        ……

如下就会出现“AttributeError: 'LSTM' object has no attribute 'f'”的问题:

class LSTM(object):

def __init__(self, a, b, c, d, e, f):

        super(LSTM, self).__init__()
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.e = e
        self.add_in_layer()
        self.add_lstm_layer()

        self.add_out_layer()

        self.f = f

        ……



你可能感兴趣的:(python)