1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
class Test(object):
def __str__(self): return 'this is a test class'
def __getattr__(self, key): return '这个key:{}并不存在'.format(key)
def __setattr__(self, key, value): self.__dict__[key] = value print(self.__dict__)
def __call__(self, a): print('call func will start') print(a)
t = Test() print(t)
print(t.a) print(t.b) t.name = '小慕' print(t.name) t('dewei')
class Test2(object): def __init__(self, attr=''): self.__attr = attr
def __call__(self, name): return name
def __getattr__(self, key): if self.__attr: key = '{}.{}'.format(self.__attr, key) else: key = key print(key) return Test2(key)
t2 = Test2() name = t2.a.b.c('dewei') print(name)
result = t2.name.age.sex('ok') print(result)
|