The Python Oracle

Pylint error with abstract member variable

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Civilization

--

Chapters
00:00 Question
00:46 Accepted answer (Score 5)
01:36 Answer 2 (Score 5)
01:50 Answer 3 (Score 1)
02:10 Thank you

--

Full question
https://stackoverflow.com/questions/1653...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pylint

#avk47



ACCEPTED ANSWER

Score 5


It is best to define name in A. Consider somebody (or you in couple weeks) wants to inherit from A and implements abstract_function:

class C(A):
    def abstract_function(self):
        print 'This is not an abstract class'

Now the following will raise an error even though nothing in C seems to be wrong:

c = C()
c.random_function()

If you are using self.name in A it should be defined there (and let's say it should default to something sensible saying it's not ready to use):

class A(object):
    name = None
    def random_function(self):
        print self.name

This will make your code cleaner/less error-prone and you will also get rid of the pylint error.




ANSWER 2

Score 5


If you suffix A with Mixin, pylint will not report it




ANSWER 3

Score 1


In your case I'd could use the following option:

pylint solution.py --generated-members=name

However, it's better to consider adding name = None to the base class.