python:在父类的方法中,调用该类的类方法,但绑定到子类

发布于 2024-12-06 08:25:23 字数 465 浏览 3 评论 0原文

假设我有以下代码:

class Parent(object):
    classattr1 = 'parent'
    def __init__(self):
        Parent.foo()

    @classmethod
    def foo(cls):
        print cls.classattr1

class Child(Parent):
    classattr1 = 'child'

    def foo(cls):
        raise Exception("I shouldn't be here")

Child()

Parent.__init__ 中,我需要调用在 Parent 中定义的“foo”,但我需要调用绑定到 Child 的它,以便访问 cls.classattr1 实际上访问该属性,因为它在 Child 中被覆盖。有什么想法如何做到这一点?

Say I have the following code:

class Parent(object):
    classattr1 = 'parent'
    def __init__(self):
        Parent.foo()

    @classmethod
    def foo(cls):
        print cls.classattr1

class Child(Parent):
    classattr1 = 'child'

    def foo(cls):
        raise Exception("I shouldn't be here")

Child()

In Parent.__init__, I need to call 'foo' that is defined within Parent, but I need to call it bound to Child, so that accessing cls.classattr1 will actually access the attribute as it is overridden in Child. Any ideas how to do this?

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

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

发布评论

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

评论(3

会发光的星星闪亮亮i 2024-12-13 08:25:23

这是一个选项:

class Parent(object):
    classattr1 = 'parent'
    def __init__(self):
        Parent.foo(self)

    def foo(self):
        print self.classattr1     # or self.__class__.classattr1

class Child(Parent):
    classattr1 = 'child'
    def foo(cls):
        raise Exception("I shouldn't be here")

Child()

Parent.foo() 不再是类方法,但最终结果应该与您想要的相同。

>>> c = Child()    # prints 'child' by calling Parent.foo()
child
>>> c.foo()        # Child.foo() raises an exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in foo
Exception: I shouldn't be here

Here is an option:

class Parent(object):
    classattr1 = 'parent'
    def __init__(self):
        Parent.foo(self)

    def foo(self):
        print self.classattr1     # or self.__class__.classattr1

class Child(Parent):
    classattr1 = 'child'
    def foo(cls):
        raise Exception("I shouldn't be here")

Child()

Parent.foo() is not a class method anymore, but the end result should be the same as what you want.

>>> c = Child()    # prints 'child' by calling Parent.foo()
child
>>> c.foo()        # Child.foo() raises an exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in foo
Exception: I shouldn't be here
转瞬即逝 2024-12-13 08:25:23

这应该可行:

Parent.foo.im_func(Child)

但看起来有点邪恶。

This should work:

Parent.foo.im_func(Child)

but looks kinda evil.

琉璃繁缕 2024-12-13 08:25:23

您真的需要 foo 成为 classmethod 吗?如果没有,这有效:

class Parent(object):
    classattr1 = 'parent'
    def __init__(self):
        Parent.foo(self)

    def foo(self):
        print self.classattr1

class Child(Parent):
    classattr1 = 'child'
    def foo(self):
        raise AttributeError("Wrong foo!")

Child()  # prints 'child'

Do you really need foo to be a classmethod? If not, this works.:

class Parent(object):
    classattr1 = 'parent'
    def __init__(self):
        Parent.foo(self)

    def foo(self):
        print self.classattr1

class Child(Parent):
    classattr1 = 'child'
    def foo(self):
        raise AttributeError("Wrong foo!")

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