帮助Python继承
给定一个任意对象:
class Val(object):
def __init__(self):
this_val = 123
我想创建一个抽象基类,它有一个 Val()
属性:
class A(object):
foo = Val()
我希望当我的孩子从该类继承时,他们会获得 的副本>Val()
。例如:
class B(A):
pass
class C(A):
pass
我期望出现以下行为:
>>> b = B()
>>> c = C()
>>> c.foo.this_val = 456
>>> b.foo.this_val
123
但是我得到:
>>> b.this_val
456
我知道我可以将 self.foo = Val()
放入 init 中来实现该行为,但我要求 foo 保留为一个属性(它是 django 中的模型管理器)。任何人都可以建议解决这个问题吗?
编辑:我确实需要能够将值作为类属性访问,所以我想要的行为是:
>>> C.foo.this_val = 456
>>> B.foo.this_val
123
Given an arbitrary object:
class Val(object):
def __init__(self):
this_val = 123
I want to create an abstract base class which has an attribute that is a Val()
:
class A(object):
foo = Val()
I would expect that when my children inherit from that class, they would get copies of Val()
. For example:
class B(A):
pass
class C(A):
pass
I would expect the following behavior:
>>> b = B()
>>> c = C()
>>> c.foo.this_val = 456
>>> b.foo.this_val
123
But instead I get:
>>> b.this_val
456
I understand that I could just self.foo = Val()
into the init to achieve that behavior, but I have a requirement that foo remain an attribute (it is a model manager in django). Can anyone suggest a work around for this?
EDIT: I really need to be able to access the value as a class attribute, so my desired behavior is:
>>> C.foo.this_val = 456
>>> B.foo.this_val
123
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
属性
foo
仅存在于A
上。您必须使用元类向每个类添加新的Val
。The attribute
foo
only exists onA
. You will have to use a metaclass to add a newVal
to each class.也许使用 descriptor 会满足您的要求:
现在,只要您确保您没有设置任何对象的实例属性“foo”,它们将具有每个子类各自的类属性:
编辑:通过我几个小时前所做的编辑,更改了键入
__get__
为objtype
而不是obj.__class__
,这在直接访问类属性时也有效:Maybe using a descriptor would suit your requirements:
Now, as long as you make sure you don't set the instance attribute "foo" of any of your objects, they will have a class attribute that is individual to each subclass:
EDIT: With the edit I made some hours ago, changing the key in
__get__
to beobjtype
instead ofobj.__class__
, this also works when accessing the class attributes directly:两者都做。
将其设为类属性,同时在 __init__ 函数中将其初始化为新实例。这样,存储的引用就不是共享的。
Do both.
Make it a class attribute, but also initialize it to a fresh instance in the
__init__
function. That way the reference stored isn't a shared one.