通过继承的静态方法更改不可变类型 class 属性
Changing immutable type class attributes by way of an inherited static method
我是 Python 的新手,确实是 OOP 的新手。我有一个派生的 class,它从基础 class 继承了几个 class 属性,其中一些是可变类型,而另一些是不可变的。派生的 class 还继承了一个静态方法,该方法将 class 属性重置为其原始值。
这在基础 class 中运行良好,并且在涉及可变类型 class 属性的派生 class 中不是问题。问题出现在不可变对象上。我知道我可能不得不完全重新设计它,但它确实提出了一个关于继承的问题,我找不到满意的答案。
下面是对上述问题的简单说明:
class Parent(object):
boolean_attribute = True
list_attribute = ['1', '2', '3']
@staticmethod
def static_reset():
Parent.boolean_attribute = True
Parent.list_attribute = ['1', '2', '3']
class Child(Parent):
pass
print(Parent.list_attribute)
Parent.list_attribute.remove('1')
print(Parent.list_attribute)
Parent.static_reset()
print(Parent.list_attribute)
print(Parent.boolean_attribute)
Parent.boolean_attribute = False
print(Parent.boolean_attribute)
Parent.static_reset()
print(Parent.boolean_attribute)
print(Child.list_attribute)
Child.list_attribute.remove('1')
print(Child.list_attribute)
Child.static_reset()
print(Child.list_attribute)
print(Child.boolean_attribute)
Child.boolean_attribute = False
print(Child.boolean_attribute)
Child.static_reset()
print(Child.boolean_attribute)
上面的输出如下:
['1', '2', '3']
['2', '3']
['1', '2', '3']
True
False
True
['1', '2', '3']
['2', '3']
['1', '2', '3']
True
False
False
请注意,static_reset 方法在最后一种情况下不起作用。
如果能深入了解这里发生的事情,我们将不胜感激。
当获取 一个属性,但该属性不存在时,将查询整个class 层次结构。这就是继承在 Python.
中的工作方式
但是当设置一个属性时,它是直接在对象(实例或class)上完成的。这就是覆盖的工作方式。
您设置了 Child.boolean_attribute
属性,之前它不存在。这不同于改变被list_attribute
引用的列表;您永远不会 设置 该属性。 Child.list_attribute
仅 获取 属性,之后您在列表对象上使用 list.remove()
方法(不是 Child
或 Parent
)更改内容。
如果您实际设置 list_attribute
:
,您会看到相同的行为
Child.list_attribute = ['foo', 'bar']
您可以在使用 vars()
function:
时看到这种情况
>>> class Parent(object):
... boolean_attribute = True
... list_attribute = ['1', '2', '3']
...
>>> class Child(Parent):
... pass
...
>>> vars(Child)
dict_proxy({'__module__': '__main__', '__doc__': None})
>>> vars(Parent)
dict_proxy({'boolean_attribute': True, '__module__': '__main__', 'list_attribute': ['1', '2', '3'], '__dict__': <attribute '__dict__' of 'Parent' objects>, '__weakref__': <attribute '__weakref__' of 'Parent' objects>, '__doc__': None})
>>> Child.boolean_attribute = False
>>> vars(Child)
dict_proxy({'boolean_attribute': False, '__module__': '__main__', '__doc__': None})
注意 Child
现在也有一个 boolean_attribute
条目。
我是 Python 的新手,确实是 OOP 的新手。我有一个派生的 class,它从基础 class 继承了几个 class 属性,其中一些是可变类型,而另一些是不可变的。派生的 class 还继承了一个静态方法,该方法将 class 属性重置为其原始值。
这在基础 class 中运行良好,并且在涉及可变类型 class 属性的派生 class 中不是问题。问题出现在不可变对象上。我知道我可能不得不完全重新设计它,但它确实提出了一个关于继承的问题,我找不到满意的答案。
下面是对上述问题的简单说明:
class Parent(object):
boolean_attribute = True
list_attribute = ['1', '2', '3']
@staticmethod
def static_reset():
Parent.boolean_attribute = True
Parent.list_attribute = ['1', '2', '3']
class Child(Parent):
pass
print(Parent.list_attribute)
Parent.list_attribute.remove('1')
print(Parent.list_attribute)
Parent.static_reset()
print(Parent.list_attribute)
print(Parent.boolean_attribute)
Parent.boolean_attribute = False
print(Parent.boolean_attribute)
Parent.static_reset()
print(Parent.boolean_attribute)
print(Child.list_attribute)
Child.list_attribute.remove('1')
print(Child.list_attribute)
Child.static_reset()
print(Child.list_attribute)
print(Child.boolean_attribute)
Child.boolean_attribute = False
print(Child.boolean_attribute)
Child.static_reset()
print(Child.boolean_attribute)
上面的输出如下:
['1', '2', '3']
['2', '3']
['1', '2', '3']
True
False
True
['1', '2', '3']
['2', '3']
['1', '2', '3']
True
False
False
请注意,static_reset 方法在最后一种情况下不起作用。
如果能深入了解这里发生的事情,我们将不胜感激。
当获取 一个属性,但该属性不存在时,将查询整个class 层次结构。这就是继承在 Python.
中的工作方式但是当设置一个属性时,它是直接在对象(实例或class)上完成的。这就是覆盖的工作方式。
您设置了 Child.boolean_attribute
属性,之前它不存在。这不同于改变被list_attribute
引用的列表;您永远不会 设置 该属性。 Child.list_attribute
仅 获取 属性,之后您在列表对象上使用 list.remove()
方法(不是 Child
或 Parent
)更改内容。
如果您实际设置 list_attribute
:
Child.list_attribute = ['foo', 'bar']
您可以在使用 vars()
function:
>>> class Parent(object):
... boolean_attribute = True
... list_attribute = ['1', '2', '3']
...
>>> class Child(Parent):
... pass
...
>>> vars(Child)
dict_proxy({'__module__': '__main__', '__doc__': None})
>>> vars(Parent)
dict_proxy({'boolean_attribute': True, '__module__': '__main__', 'list_attribute': ['1', '2', '3'], '__dict__': <attribute '__dict__' of 'Parent' objects>, '__weakref__': <attribute '__weakref__' of 'Parent' objects>, '__doc__': None})
>>> Child.boolean_attribute = False
>>> vars(Child)
dict_proxy({'boolean_attribute': False, '__module__': '__main__', '__doc__': None})
注意 Child
现在也有一个 boolean_attribute
条目。