gpt4 book ai didi

python - Python 何时从实例 __dict__ 返回到类 __dict__?

转载 作者:行者123 更新时间:2023-12-05 08:35:48 25 4
gpt4 key购买 nike

请看下面的片段:

class Foo:
class_var = "hi"

foo = Foo()
assert foo.class_var is Foo.class_var
assert "class_var" in Foo.__dict__
assert "class_var" not in foo.__dict__

这里的所有断言都通过了,尽管我不确定身份断言通过是否令人惊讶。

Python 何时以及如何从实例 __dict__ 回退到类 __dict__

最佳答案

根据(已经提到)[Python.Docs]: Data model (强调是我的):

Custom classes

Custom class types are typically created by class definitions (see section Class definitions). A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., C.x is translated to C.__dict__["x"] (although there are a number of hooks which allow for other means of locating attributes). When the attribute name is not found there, the attribute search continues in the base classes.

...

Class instances

A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes.

...

Invoking Descriptors

...

The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, a.x has a lookup chain starting with a.__dict__['x'], then type(a).__dict__['x'], and continuing through the base classes of type(a) excluding metaclasses.

However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called.

在类定义内部(但在初始化器(或其他方法)之外)定义的属性称为类属性,并且绑定(bind)到类本身而不是它的实例。它类似于来自 C++Javastatic 成员。 [Python.Docs]: Compound statements - Class definitions状态(强调 仍然是我的):

Programmer’s note: Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with self.name = value. Both class and instance attributes are accessible through the notation “self.name”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. Descriptors can be used to create instance variables with different implementation details.

因此,属性查找顺序可以总结如下(按升序遍历,当找到属性名称时只返回其值(因此忽略其余条目))。 (内置)__getattribute__ 方法执行的第一步:

  1. 描述符(如果有的话 - 请注意它们的存在也可以间接触发(由其他功能))

  2. 实例命名空间(foo.__dict__)

  3. 实例类命名空间(Foo.__dict__)

  4. 实例类基类命名空间(e.__dict__ for e in Foo.__mro__)

  5. 自定义 __getattr__ 方法可能返回的任何内容

以上是通常发生的情况,因为 Python 是高度可定制的,可以更改(例如 __slots__)。

对于确切的行为,您可以查看源代码 ( [GitHub]: python/cpython - (main) cpython/Objects ):

  • typeobject.c:type_getattro(可选:super_getattroslot_tp_getattro)

  • object.c:_PyObject_GenericGetAttrWithDict

这是一个可以解决问题的示例(希望如此)。

code00.py:

#!/usr/bin/env python

import sys
from pprint import pformat as pf


def print_dict(obj, header="", indent=0, filterfunc=lambda x, y: not x.startswith("__")):
if not header:
header = getattr(obj, "__name__", None)
if header:
print("{:}{:}.__dict__:".format(" " * indent, header))
lines = pf({k: v for k, v in getattr(obj, "__dict__", {}).items() if filterfunc(k, v)}, sort_dicts=False).split("\n")
for line in lines:
print("{:}{:}".format(" " * (indent + 1), line))
print()


class Descriptor:
def __init__(self, name):
self.name = name

def __get__(self, instance, cls):
print("{:s}.__get__".format(self.name))

def __set__(self, instance, value):
print("{:s}.__set__ - {:}".format(self.name, value))

def __delete__(self, instance):
print("{:s}.__delete__".format(self.name))


class Demo:
cls_attr0 = 3.141593
cls_attr1 = Descriptor("cls_attr1")

'''
def __getattribute__(self, name):
print("__getattribute__:", self, name)
return super().__getattribute__(name)
'''

'''
def __getattr__(self, name):
print("__getattr__:", self, name)
return "something dummy"
'''

def __init__(self):
self.inst_attr0 = 2.718282


def main(*argv):
print("ORIGINAL")
demos = [Demo() for _ in range(2)]
demo0 = demos[0]
demo1 = demos[1]
print_dict(Demo)
print_dict(demo0, header="demo0")
print("\ndemo0 attrs:", demo0.cls_attr0, demo0.cls_attr1, demo0.inst_attr0)
print_dict(demo1, header="\ndemo1")
print("\ndemo1 attrs:", demo1.cls_attr0, demo1.cls_attr1, demo1.inst_attr0)

print("\nALTER 1ST INSTANCE OBJECT")
demo0.inst_attr0 = -3
demo0.cls_attr0 = -5

print_dict(Demo)
print_dict(demo0, header="demo0")
print("\ndemo0 attrs:", demo0.cls_attr0, demo0.cls_attr1, demo0.inst_attr0)
print_dict(demo1, header="\ndemo1")
print("\ndemo1 attrs:", demo1.cls_attr0, demo1.cls_attr1, demo1.inst_attr0)

print("\nALTER CLASS")
Demo.cls_attr0 = -7
Demo.cls_attr1 = -9
print_dict(Demo, header="Demo")
print_dict(demo1, header="demo0")
print("\ndemo0 attrs:", demo0.cls_attr0, demo0.cls_attr1, demo0.inst_attr0)
print_dict(demo1, header="\ndemo1")
print("\ndemo1 attrs:", demo1.cls_attr0, demo1.cls_attr1, demo1.inst_attr0)


if __name__ == "__main__":
print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32, sys.platform))
rc = main(*sys.argv[1:])
print("\nDone.")
sys.exit(rc)

输出:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q072399556]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" code00.py
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32

ORIGINAL
Demo.__dict__:
{'cls_attr0': 3.141593,
'cls_attr1': <__main__.Descriptor object at 0x00000171B0B24FD0>}

demo0.__dict__:
{'inst_attr0': 2.718282}

cls_attr1.__get__

demo0 attrs: 3.141593 None 2.718282

demo1.__dict__:
{'inst_attr0': 2.718282}

cls_attr1.__get__

demo1 attrs: 3.141593 None 2.718282

ALTER 1ST INSTANCE OBJECT
Demo.__dict__:
{'cls_attr0': 3.141593,
'cls_attr1': <__main__.Descriptor object at 0x00000171B0B24FD0>}

demo0.__dict__:
{'inst_attr0': -3, 'cls_attr0': -5}

cls_attr1.__get__

demo0 attrs: -5 None -3

demo1.__dict__:
{'inst_attr0': 2.718282}

cls_attr1.__get__

demo1 attrs: 3.141593 None 2.718282

ALTER CLASS
Demo.__dict__:
{'cls_attr0': -7, 'cls_attr1': -9}

demo0.__dict__:
{'inst_attr0': 2.718282}


demo0 attrs: -5 -9 -3

demo1.__dict__:
{'inst_attr0': 2.718282}


demo1 attrs: -7 -9 2.718282

Done.

关于python - Python 何时从实例 __dict__ 返回到类 __dict__?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72399556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com