1class A:
2
3    var = 132
4
5    def __init__(self):
6        self.var2 = 34
7
8    def meth(self, i):
9        return 42 + i
10
11
12a = A()
13print(hasattr(a, "var"))
14print(hasattr(a, "var2"))
15print(hasattr(a, "meth"))
16print(hasattr(a, "_none_such"))
17print(hasattr(list, "foo"))
18
19class C:
20
21    def __getattr__(self, attr):
22        if attr == "exists":
23            return attr
24        elif attr == "raise":
25            raise Exception(123)
26        raise AttributeError
27
28c = C()
29print(hasattr(c, "exists"))
30print(hasattr(c, "doesnt_exist"))
31
32# ensure that non-AttributeError exceptions propagate out of hasattr
33try:
34    hasattr(c, "raise")
35except Exception as er:
36    print(er)
37
38try:
39    hasattr(1, b'123')
40except TypeError:
41    print('TypeError')
42
43try:
44    hasattr(1, 123)
45except TypeError:
46    print('TypeError')
47