Search code examples
pythoninheritancepylintlinter

Python 3: Accessing Parent Attributes, Linter Error


I have some trouble accessing parent attributes appropriately in Python 3.8 with VS Code and PyLint.

PyLint gives me an Error in the second file in the pseudo code listed below.

file 1:

class Parent:
    def __init__(self, parentAttr):
        self.parentAttr= parentAttr

file 2:

class Child(Parent):
    def __init__(self, parentAttr, childAttr):
        super().__init__(parentAttr)
        self.childAttr= childAttr

    def anyFunction(self):

In the next line (line 7) the Linter underlines "self." in red and says:

Access to member 'parentAttr' before its definition line 8. pylint(access-member-before-definition)

        tmp = self.parentAttr
        self.parentAttr= tmp 

Code works as it should. But what is the way how I should do such things correctly, without getting this error?


Solution

  • Are you sure you don't have a typo on line 7 of file 2, or on line 3 of file 1? Are you importing the "Parent" class you think you're importing? I tried reproducing your issue and vscode+pylint does not complain about your code on my machine.

    enter image description here

    I honestly don't see why it would, You're doing the right thing, at least in the pseudo-code you gave. If it complains, it must be because of a typo somewhere, and a potential bug in your code that you haven't detected yet.