How to access a method inside a class?

眉间皱痕 提交于 2020-01-06 06:30:49

问题


Environment: Python 2.7 (Might be related).

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)

How do I save the __repr__ method of the class?

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

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

EDIT: 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>"

you can see that super().__repr__ is not the same as the original __repr__


回答1:


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.

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'

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__()

You can test out this by doing

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

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

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__()

And the output will change to

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



回答2:


I think you looking for,

super().__repr__()


class A:
    # original_repr = ?__repr__

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

    def __repr__(self):
        return super().__repr__()



回答3:


def __repr__(self):
        if self.switch:
            return "Hello"
        return super().__repr__()


来源:https://stackoverflow.com/questions/56129685/how-to-access-a-method-inside-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!