首页 > 解决方案 > How to bring variables of super method into child method in Python3

问题描述

I am implementing a subclass Child where an overwritten method some_method calls the parent method first. I want to use variable a from the parent method in the child method. Is there a neat way to do this without having to modify the code of Parent?

class Parent:
    def __init__(self):
        pass

    def some_method(self):
        a = 0


class Child(Parent):
    def __init__(self):
        super().__init__()

    def some_method(self):
        super().some_method()
        b = a - 1   # Here I would like to keep using `a`

标签: pythonpython-3.x

解决方案


推荐阅读