Python 3中的继承和__dict__

用户名

我正在阅读python 3中有关OOP的博客文章其中有:

class Door:
    colour = 'brown'

    def __init__(self, number, status):
        self.number = number
        self.status = status

    @classmethod
    def knock(cls):
        print("Knock!")

    def open(self):
        self.status = 'open'

    def close(self):
        self.status = 'closed'

class SecurityDoor:
    locked = True

    def __init__(self, number, status):
        self.door = Door(number, status)

    def open(self):
        if self.locked:
            return
        self.door.open()

    def __getattr__(self, attr):
        return getattr(self.door, attr)

当我做:

>>> sdoor = SecurityDoor(1, 'closed')
>>> sdoor.__dict__
{'door': <__main__.Door object at 0x0279A250>}

为什么在属性字典中没有“ locked:True”?毕竟,“锁定”是一个属性

Warvariuc

为什么在属性字典中没有“ locked:True”?

因为您正在检查实例dict,但它locked是一个类属性。您可以使用找到它self.__class__.__dict__

如果需要查看所有属性,包括继承的和类的属性,请使用insect.getmembers()或内置dir函数:

>>> import inspect
>>> class A(object):
...     b = 1
...     def __init__(self):
...         self.c = 1
...
>>> a = A()

>>> inspect.getmembers(a)
[('__class__', <class '__main__.A'>), ('__delattr__', <method-wrapper '__delattr__' of A object at 0x10d47f1d0>), ('__dict__', {'c': 1}), ('__doc__', None), ('__format__', <built-in method __format__ of A object at 0x10d47f1d0>), ('__getattribute__', <method-wrapper '__getattribute__' of A object at 0x10d47f1d0>), ('__hash__', <method-wrapper '__hash__' of A object at 0x10d47f1d0>), ('__init__', <bound method A.__init__ of <__main__.A object at 0x10d47f1d0>>), ('__module__', '__main__'), ('__new__', <built-in method __new__ of type object at 0x10d0d7280>), ('__reduce__', <built-in method __reduce__ of A object at 0x10d47f1d0>), ('__reduce_ex__', <built-in method __reduce_ex__ of A object at 0x10d47f1d0>), ('__repr__', <method-wrapper '__repr__' of A object at 0x10d47f1d0>), ('__setattr__', <method-wrapper '__setattr__' of A object at 0x10d47f1d0>), ('__sizeof__', <built-in method __sizeof__ of A object at 0x10d47f1d0>), ('__str__', <method-wrapper '__str__' of A object at 0x10d47f1d0>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x7fc6b8638070>), ('__weakref__', None), ('b', 1), ('c', 1)]

>>> dir(a)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'b', 'c']

>>> a.__class__.__dict__
dict_proxy({'__module__': '__main__', 'b': 1, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None, '__init__': <function __init__ at 0x10d4791b8>})

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python类继承和__dict__查找

来自分类Dev

在Python中动态添加类__dict__属性

来自分类Dev

python2和python3中dict的__repr __()

来自分类Dev

出于好奇:为什么python3不使用__dict__作为默认的相等实现?

来自分类Dev

Python ** obj .__ dict__ equilavent命令

来自分类Dev

python:打印__dict__:是否可以按照类中列出的顺序打印?

来自分类Dev

python:打印__dict__:是否可以按照类中列出的顺序打印?

来自分类Dev

Python和多重继承中的方法顺序

来自分类Dev

从python中的'heapq'和'deque'继承吗?

来自分类Dev

Python和构造函数中的多重继承

来自分类Dev

从抽象基类继承时,为什么__slots__在Python 2和3中的行为不同

来自分类Dev

python3中'super'的继承

来自分类Dev

如何在 Python3 中引用 dict() 和 list() 的组合?

来自分类Dev

Python继承和变量

来自分类Dev

Python:如何从类变量访问class .__ dict__?

来自分类Dev

使用class时的Python .format()KeyError .__ dict__

来自分类Dev

python cffi库对象自省导致__dict__对象更改

来自分类Dev

Python:如何从类变量访问class .__ dict__?

来自分类Dev

Python,用 __dict__ 输出连接字符串

来自分类Dev

Python 2.7和3之间的多重继承差异

来自分类常见问题

Python中私有和受保护方法的继承

来自分类Dev

Python中的文本冒险:使用类和继承

来自分类Dev

如何在python中同时使用封装和继承

来自分类Dev

Python中的动态继承

来自分类Dev

python 2.7中的继承

来自分类Dev

python中的类继承

来自分类Dev

Python中的混合继承

来自分类Dev

Python中的继承

来自分类Dev

使用txtfile创建从dict继承的类-python