__str__
print
当前实例化对象的时候, 会返回改函数的return
信息def __str__(self):
return str_type
__gatattr__
def __gatattr__(self, key):
print(something...)
key
: 调用任意不存在的属性名# coding:utf-8
class Test(object):
def __str__(self):
return 'this is a test class'
def __getattr__(self, key):
# print('这个key:{}并不存在'.format(key))
return '这个key:{}并不存在'.format(key)
t = Test()
print(t)
# print(t.a)
print(t.a)
print(t.b)
__setattr__
def __settattr__(self, key, value):
self.__dict__[key] = value
key
当前的属性名value
当前的参数对应的值__call__
def __call__(self, *args, **kwargs):
print('call will start')
# coding:utf-8
class Test(object):
def __str__(self):
return 'this is a test class'
def __getattr__(self, key):
# print('这个key:{}并不存在'.format(key))
return '这个key:{}并不存在'.format(key)
def __setattr__(self, key, value):
# print(key, value)
# if key not in self.__dict__:
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.a)
print(t.b)
t.name = '小慕'
print(t.name)
t('dewei')
# t.a.b.c 链式操作
class Test2(object):
def __init__(self, attr=''):
self.__attr = attr
def __call__(self, name):
# print('key is {}'.format(self.__attr))
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)