如何访问类中的方法? [英] How to access a method inside a class?

查看:69
本文介绍了如何访问类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:Python 2.7(可能相关)。

Environment: Python 2.7 (Might be related).

例如,我想将类称为原始 __ repr __

for example, I want to call class original __repr__ method depending on if an attribute on instance has been set.

class A(object):
    # original_repr = ?__repr__

    def __init__(self, switch):
        self.switch = switch

    def __repr__(self):
        if self.switch:
            return self._repr()
        else:
            # return saved original __repr__ method.


def custom_repr(self):
    pass

a = A()
a._repr = MethodType( custom_repr, a, A)

如何保存 __ repr __ 类的方法?

How do I save the __repr__ method of the class?

显然我不能像实例中那样使用 self

obviously I can't use self like in an instance.

也不能使用 A。__repr __ 也不能使用,因为 A

Can't use A.__repr__ either, since A itself is not defined at that time.

编辑:有人建议使用 super().__ repr __ ,我在代码中对其进行了测试:

someone suggested using super().__repr__, however, I tested it in code:

class C(object):
    pass

class D(object):
    def __repr__(self):
        return super(self.__class__).__repr__()

c = C()
d = D()

# repr(c) --> '<__main__.C object at 0x0000000003AEFBE0>'
# repr(d) --> "<super: <class 'D'>, NULL>"

您可以看到 super()。__repr __ 与原始 __ repr __

推荐答案

通过 super().__ repr __()在超类的 __ repr __ 方法中进行操作。下面将通过子类化一个新类来显示示例。

You can fallback on the __repr__ method of the super-class via super().__repr__(). I will show an example below by subclassing a new class.

因此,如果我们有一个父类 B类,如下所示,并定义了自己的 __ repr __

So if we have a parent class B as follows, with it's own __repr__ defined

class B:

    def __repr__(self):

        return 'This is repr of B'

然后我们像以前一样有一个孩子 A类,它继承自 B ,返回到B的 __ repr __ ,如下所示

And then we have a child class A as before, which inherits from B, it can fall back to __repr__ of B as follows

class A(B):

    def __init__(self, switch):
        super().__init__()
        self.switch = switch

    def __repr__(self):
        #If switch is True, return repr of A
        if self.switch:
            return 'This is repr of A'
        #Else return repr of B by calling super
        else:
            return super().__repr__()

您可以测试一下

print(A(True))
print(A(False))

如预期的那样,第一种情况将触发A的 __ repr __ ,第二种情况将触发B的 __ repr __

As expected, the first case will trigger the __repr__ of A, and the second case will trigger the __repr__ of B.

This is repr of A
This is repr of B

如果A只是从对象继承的普通类,则代码将更改为

If A is just a normal class which inherits from object, the code will change to

class A:

    def __init__(self, switch):
        super().__init__()
        self.switch = switch

    def __repr__(self):
        #If switch is True, return repr of A
        if self.switch:
            return 'This is repr of A'
        #Else return repr of superclass by calling super
        else:
            return super().__repr__()

输出将更改为

print(A(True))
#This is repr of A
print(A(False))
#<__main__.A object at 0x103075908>

这篇关于如何访问类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆