何时以及为何使用 self.__dict__ 而不是 self.variable

问题描述 投票:0回答:4

我试图理解下面使用此类的一些代码:

class Base(object):

    def __init__(self, **kwargs):
        self.client = kwargs.get('client')
        self.request = kwargs.get('request')
    ...

    def to_dict(self):
        data = dict()

        for key in iter(self.__dict__): # <------------------------ this
            if key in ('client', 'request'):
                continue

            value = self.__dict__[key]
            if value is not None:
                if hasattr(value, 'to_dict'):
                    data[key] = value.to_dict()
                else:
                    data[key] = value
        return data

我知道它会将关键字参数传递给

Base
类,例如
Base(client="foo", request="bar")

我的困惑是,为什么它使用

self.__dict__
__init__
内的变量转换为字典(例如
{"client": "foo", "request": "bar"}
),而不是仅通过
self.client
self.request
在其他方法中调用它们?何时以及为什么我应该使用
self.__dict__
来代替?

python class dictionary oop
4个回答
17
投票

几乎所有时候,您都不应该使用

self.__dict__

如果您正在访问像

self.client
这样的属性,即属性名称是已知且固定的,那么它与
self.__dict__['client']
之间的唯一区别是后者不会在类上查找该属性(如果该属性丢失)实例。很少有任何理由这样做,但差异如下所示:

>>> class A:
...     b = 3 # class attribute, not an instance attribute
... 
>>> A.b # the class has this attribute
3
>>> a = A()
>>> a.b # the instance doesn't have this attribute, fallback to the class
3
>>> a.__dict__['b'] # the instance doesn't have this attribute, but no fallback
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'b'

self.__dict__
的主要用例是当您不想想要访问固定的已知属性名称时。在几乎所有代码中,您“总是”知道要访问哪个属性;如果您确实需要使用未知字符串动态查找某些内容,您应该自己创建一个字典,并编写 self.that_dict[key] 而不是
self.__dict__[key]

因此,您真正应该使用

__dict__

的唯一时间是当您编写需要工作的代码时,无论实例可能具有哪些属性;即,您特别想要即使更改类的结构或其属性名称也能工作的代码,或者能够跨具有不同结构的多个类工作的代码。我将在下面展示一个例子。


__repr__

方法

__repr__

方法旨在返回表示实例的字符串,以方便程序员使用 REPL 时。出于调试/测试目的,该字符串通常包含有关对象状态的信息。这是实现它的常见方法:


class Foo: def __init__(self, foo, bar, baz): self.foo = foo self.bar = bar self.baz = baz def __repr__(self): return 'Foo({!r}, {!r}, {!r})'.format(self.foo, self.bar, self.baz)

这意味着如果您编写 
obj = Foo(1, 'y', True)

来创建实例,那么

repr(obj)
将是字符串
"Foo(1, 'y', True)"
,这很方便,因为它显示了实例的整个状态,而且字符串本身就是创建实例的 Python 代码相同的状态。

但是上述实现存在一些问题:如果类的属性发生变化,我们就必须更改它,它不会为子类的实例提供有用的结果,并且我们必须为具有不同属性的不同类编写大量类似的代码。如果我们使用

__dict__

来代替,我们可以解决所有这些问题:


def __repr__(self): return '{}({})'.format( self.__class__.__name__, ', '.join('{}={!r}'.format(k, v) for k, v in self.__dict__.items()) )

现在
repr(obj)

将是

Foo(foo=1, bar='y', baz=True)
,它也显示了实例的整个状态,并且也是可执行的Python代码。如果
__repr__
的结构发生变化,这个通用
Foo
方法仍然有效,它可以通过继承在多个类之间共享,并且它为任何其属性被
__init__
接受为关键字参数的类返回可执行的 Python 代码。
    


2
投票
__dict__

保存类中的

all
变量。参加以下课程: class A(): def __init__(self, foo): self.foo = foo def new_var(self, bar): self.bar = bar

那么在这种情况下,请注意:

a = A('var1') print(a.__dict__) # {'foo': 'var1'} b = A('var1') b.new_var('var2') b.foobar = 'var3' print(b.__dict__) # {'foo': 'var1', 'bar': 'var2', 'foobar': 'var3'}

在你的情况下,你可以选择或者。 
__dict__

是在调用该类的当前实例中获取属于该类的所有变量的好方法。您可以在

__dict__
此处
查看文档。


0
投票

__dict__ 用于检查对象具有哪些实例变量(数据属性)。 所以,如果有下面的

Person

类:

class Person:
    x1 = "Hello"
    x2 = "World"
    
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def test1(self):
        print(self.__dict__) # Here
        
    @classmethod
    def test2(cls):
        pass
    
    @staticmethod
    def test3():
        pass

obj = Person("John", 27)    
obj.test1() # Here

__dict__ 获取 name

age
及其在字典中的值,如下所示:
{'name': 'John', 'age': 27} # Here

并且,如果
实例化后添加新的实例变量

gender

,如下所示:
# ... obj= Person("John", 27) obj.test1() obj.gender = "Male" # Here obj.test1()

__dict__ 获取 name

age
gender
及其在字典中的值,如下所示:
{'name': 'John', 'age': 27}
{'name': 'John', 'age': 27, 'gender': 'Male'} # Here



0
投票
© www.soinside.com 2019 - 2024. All rights reserved.