logo
Loading...

getattr(object, name)的用法與後面加了()的用意 - Cupoy

在教學中寫到:def spam(s): for attr in ['isnumeric...

getattr(object, name)的用法與後面加了()的用意

2020/11/19 09:24 下午
翁如萱
觀看數:53
回答數:2
收藏數:1

在教學中寫到: def spam(s): for attr in ['isnumeric', 'isdecimal', 'isdigit']: print(attr, getattr(s, attr)()) 而菜鳥教程寫到,getattr用法是->getattr(object, attr), 並回傳屬性值 >>>class A(object): ... bar = 1 ... >>> a = A() >>> getattr(a, 'bar') # 获取属性 bar 值 1 想問一下教學中getattr(s, attr)()後面的()用處為何?是s.attr()的概念嗎?(e.g. : s.isnumeric())