“追加”而不是覆盖超类实例变量

发布于 2024-12-08 20:27:30 字数 164 浏览 0 评论 0原文

在 Python 中,有没有办法做这样的事情?

class A():
    var = "1, 2, 3"

class B():
    var = ... ", 4"


instance = B()
instance.var # = 1, 2, 3, 4

Is there a way, in Python, to do something like this?

class A():
    var = "1, 2, 3"

class B():
    var = ... ", 4"


instance = B()
instance.var # = 1, 2, 3, 4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缘字诀 2024-12-15 20:27:30

问题中给出的示例既不使用超类也不使用实例变量。从问题的标题来看,想必您想同时使用两者。下面是它如何工作的示例:

class A(object):
  def __init__(self):
    self.var = "1,2,3"

class B(A):
  def __init__(self):
    super(B,self).__init__()
    self.var += ",4,5"

print A().var
print B().var

请注意,您的示例只是使用类似于命名空间的类将属性分配给不相关的类。这是完全合理的,但不完全符合你的要求:

class A:
  var = "1,2,3"

class B:
  var = A.var + ",4,5"

print A.var
print B.var

The example given in the question uses neither a superclass nor an instance variable. From the title of the question, presumably you wanted to use both. Here's an example of how it would work:

class A(object):
  def __init__(self):
    self.var = "1,2,3"

class B(A):
  def __init__(self):
    super(B,self).__init__()
    self.var += ",4,5"

print A().var
print B().var

Note that your example simply assigns attributes to unrelated classes, using the classes sort-of like namespaces. Which is perfectly reasonable, but not quite what you asked:

class A:
  var = "1,2,3"

class B:
  var = A.var + ",4,5"

print A.var
print B.var
旧梦荧光笔 2024-12-15 20:27:30

你不能追加,你可以替换,但你可以用包含原始内容的表达式替换:

class A(object):
    var = "1, 2, 3"

class B(A):
    var = A.var + ", 4"


instance = B()
instance.var # = 1, 2, 3, 4

这只是为了简单的用途,但更有趣的解决方案可能是使用描述符;最简单的方法是使用 property

class A(object):
    var = "1, 2, 3"

class B(A):
    _subvar = ", 4"
    @property
    def var(self):
        return super(B, self).var + self._subvar


instance = B()
instance.var # = 1, 2, 3, 4

它适用于 B 的实例,但不适用于 B 本身(如第一个示例)。要恢复它,您必须实现自己的描述符:

class Appender(object):
    def __init__(self, cls, attr):
        self.cls = cls
        self.attr = attr
    def __get__(self, instance, owner):
        var = super(self.cls, owner).var
        return var + getattr(self.cls, self.attr)

class B(A):
    _realvar = ', 4'
B.var = Appender(B, '_realvar')

它在类上和在类的实例上同样有效

you can't append, you replace, but you can replace by an expression that includes the original:

class A(object):
    var = "1, 2, 3"

class B(A):
    var = A.var + ", 4"


instance = B()
instance.var # = 1, 2, 3, 4

That does it for simple uses, but a more interesting solution might be to use a descriptor; The easy way is to use property

class A(object):
    var = "1, 2, 3"

class B(A):
    _subvar = ", 4"
    @property
    def var(self):
        return super(B, self).var + self._subvar


instance = B()
instance.var # = 1, 2, 3, 4

Which works for instances of B, but not for B itself (as in the first example). To get that back, you must implement your own descriptor:

class Appender(object):
    def __init__(self, cls, attr):
        self.cls = cls
        self.attr = attr
    def __get__(self, instance, owner):
        var = super(self.cls, owner).var
        return var + getattr(self.cls, self.attr)

class B(A):
    _realvar = ', 4'
B.var = Appender(B, '_realvar')

Which works equally well on the class as it does on instances of the class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文