111:特殊属性

Python 对象中包含了很多双下划线开始和结束的属性,这些是特殊属性,有特殊用法。这 里我们列出常见的特殊属性:

# 测试特殊属性
class A:
    pass


class B:
    pass


class C(B, A):

    def __init__(self, nn):
        self.nn = nn

    def cc(self):
        print("cc")


c = C(3)

print(dir(c))
print(c.__dict__)
print(c.__class__)
print(C.__bases__)
print(C.mro())
print(A.__subclasses__())

输出结果:

D:\work\python\three\venv\Scripts\python.exe D:\work\python\three\mypy24.py 
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cc', 'nn']
{'nn': 3}
<class '__main__.C'>
(<class '__main__.B'>, <class '__main__.A'>)
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
[<class '__main__.C'>]

进程已结束,退出代码0

 

posted @ 2022-11-21 10:48  竹蜻蜓vYv  阅读(85)  评论(0编辑  收藏  举报