class People(object):
"""注解:人类"""
# 初始化函数
def __init__(self, name):
self.name = name
# 析构函数,一般解释器会自动分配和释放内存,
# 所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的。
def __del__(self):
pass
# print输出对象调用 print(people)
def __str__(self):
return "str函数被调用"
# 直接使用对象时调用 people
def __repr__(self):
return "repr函数被调用"
# 对象当做方法时调用,例people()
def __call__(self, *args, **kwargs):
print("call函数被调用")
# 用于索引操作,可如字典般使用
# 获取对象属性,people['name']
def __getitem__(self, item):
return getattr(self, item)
# 设置对象属性,people['name'] = 'Jack'
def __setitem__(self, key, value):
setattr(self, key, value)
# 删除对象属性,del people['name']
def __delitem__(self, key):
delattr(self, key)
# 正常情况下,当我们调用类的方法或属性时,如果不存在,就会报错
# 当对未定义的属性名称和实例进行点号运算时,就会用属性名作为字符串调用这个方法
def __getattr__(self, item):
if item == 'age':
return 18
# 会拦截所有属性的的赋值语句。如果定义了这个方法,self.arrt = value 就会变成self,__setattr__("attr", value)
# 当在__setattr__方法内对属性进行赋值是,不可使用self.attr = value,因为他会再次调用self,__setattr__("attr", value),则会形成无穷递归循环,最后导致堆栈溢出异常
# 应该通过对属性字典做索引运算来赋值任何实例属性,也就是使用self.__dict__['name'] = value
def __setattr__(self, key, value):
super().__setattr__(key, value)
def __delattr__(self, key):
super().__delattr__(key)
people = People('Jack')
print(people.__doc__) # 注解:人类
print(People.__doc__) # 注解:人类
print(people.__module__) # __main__
print(People.__module__) # __main__
print(people.__class__) # <class '__main__.People'>
print(People.__class__) # <class 'type'>
print(people.__dict__) # {}
# {'__module__': '__main__', '__doc__': '注解:人类',
# '__init__': <function People.__init__ at 0x00000000037957B8>,
# '__del__': <function People.__del__ at 0x000000000D8A2A60>,
# '__str__': <function People.__str__ at 0x000000000D8A2AE8>,
# '__repr__': <function People.__repr__ at 0x000000000D8A2B70>,
# '__call__': <function People.__call__ at 0x000000000D8A2BF8>,
# '__getitem__': <function People.__getitem__ at 0x000000000D8A2C80>,
# '__setitem__': <function People.__setitem__ at 0x000000000D8A2D08>,
# '__delitem__': <function People.__delitem__ at 0x000000000D8A2D90>,
# '__getattr__': <function People.__getattr__ at 0x000000000D8A2E18>,
# '__setattr__': <function People.__setattr__ at 0x000000000D8A2EA0>,
# '__delattr__': <function People.__delattr__ at 0x000000000D8A2F28>,
# '__dict__': <attribute '__dict__' of 'People' objects>,
# '__weakref__': <attribute '__weakref__' of 'People' objects>}
print(People.__dict__)
print(People) # <class '__main__.People'>
print(people) # str函数被调用
people # 啥也输出,不知道为啥
people() # call函数被调用
print(people['name']) # Jack
people['name'] = 'Tom'
print(people['name']) # Tom
del people['name']
print(people['name']) # None
print(people.age) # 18
python Class内置方法解析
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- defaultdict. 我们知道,可以通过d=defaultdict(int)来实现对value的默认复制,那么...
- argparse命令是解析命令行传递的参数工具比如命令行执行一个python命令python test.py 1...
- 对于初学者来说,python面向对象编程是比较简单易学的,但是其中有些概率可能比较模糊。最近在看《efficien...