Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does R0902 of Pylint mean? Why do we have this limit?

Tags:

python

pylint

What is behind the R0902 limit? Will too many instance attributes slow down the python interpreter or is it simply because too many instance attributes will make the class harder to understand?

like image 581
Myrfy Avatar asked Feb 03 '17 06:02

Myrfy


1 Answers

From the (DOCS):

too-many-instance-attributes (R0902):

  • Too many instance attributes (%s/%s) Used when class has too many instance attributes, try to reduce this to get a simpler (and so easier to use) class.

And then from the (Tutorial):

... but I have run into error messages that left me with no clue about what went wrong, simply because I was unfamiliar with the underlying mechanism of code theory. One error that puzzled my newbie mind was:

:too-many-instance-attributes (R0902): *Too many instance attributes (%s/%s)*

I get it now thanks to Pylint pointing it out to me. If you don’t get that one, pour a fresh cup of coffee and look into it - let your programmer mind grow!

So, yes that is not so clear.

But, as stated in the description above, with methods, classes and modules, smaller is generally better for understand-ability, re-usability and therefore manageability. When things get too large, it is often a sign that things could be refactored, to make them smaller. There is really no way to place a hard limit on this, and as disccussed in this question about how to turn this message off, pylint should not have the last word.

So, just take the message as a hint to study that section of code a bit more.

like image 135
Stephen Rauch Avatar answered Oct 06 '22 05:10

Stephen Rauch