2014-03-26 17:35:13 -04:00
|
|
|
class A:
|
|
|
|
|
|
|
|
var = 132
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.var2 = 34
|
|
|
|
|
|
|
|
def meth(self, i):
|
|
|
|
return 42 + i
|
|
|
|
|
|
|
|
|
|
|
|
a = A()
|
|
|
|
print(getattr(a, "var"))
|
|
|
|
print(getattr(a, "var2"))
|
|
|
|
print(getattr(a, "meth")(5))
|
2014-04-05 06:33:04 -04:00
|
|
|
print(getattr(a, "_none_such", 123))
|
|
|
|
print(getattr(list, "foo", 456))
|
2014-06-07 18:13:35 -04:00
|
|
|
print(getattr(a, "va" + "r2"))
|
2020-05-28 20:28:38 -04:00
|
|
|
|
|
|
|
# test a class that defines __getattr__ and may raise AttributeError
|
|
|
|
class B:
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
if attr == "a":
|
|
|
|
return attr
|
|
|
|
else:
|
|
|
|
raise AttributeError
|
|
|
|
b = B()
|
|
|
|
print(getattr(b, "a"))
|
|
|
|
print(getattr(b, "a", "default"))
|
|
|
|
print(getattr(b, "b", "default"))
|