Access local variables of overridden parent method

Viewed 1770

How can I access the local variables of a super class method in an overridden method in the subclass?

class Foo(object):
    def foo_method(self):
        x = 3

class Bar(Foo):
    def foo_method(self):
        super().foo_method()
        print(x) # Is there a way to access x, besides making x an attribute of the class?

The code below gives a NameError: name 'x' is not defined

bar = Bar()
bar.foo_method()

This isn't surprising, and it can be fixed by making x an instance attribute, but can x be accessed as-is in Bar.foo_method more directly?

1 Answers
Related