0
考慮對象customer
和屬性列表attrs
。 如何迭代列表以獲取列表中的屬性?從列表中獲取對象的屬性
class Human():
name = 'Jenny'
phone = '8675309'
customer = Human()
attrs = ['name', 'phone']
print(customer.name) # Jenny
print(customer.phone) # 8675309
for a in attrs:
print(customer.a) # This doesn't work!
print(customer[a]) # Neither does this!
我專門針對Python3(Debian的Linux的),但Python2答案將受到歡迎,以及。
大,謝謝! – dotancohen