getattr()在类方法中的使用
class Net():
def __init__(self):
self.net_list = ['G_A','G_B','D_A','D_B']
self.netG_A = 'ga'
self.netG_B = 'gb'
self.netD_A = 'da'
self.netD_B = 'db'
def print_net(self):
for net_name in self.net_list:
net = getattr(self, 'net' + net_name)
print(net)
n = Net()
n.print_net()
结果:
ga
gb
da
db