what is best practice to control "too many local variable in a function" without suppress and manipulate pylint settings?
Asked Answered
W

1

24

I'm working to make sure python code files must be 10/10 score with regular pylint and pycodestyle. but, I'm getting hard change from "too many local variable" in the functions. The functions could to split due to timing issue of whole suite. please suggest some best practice or suggestions.

Thanks in advance!!

Whitleather answered 19/11, 2019 at 4:55 Comment(4)
Could you edit your question to include some examples of functions where you get the warning?Testimonial
I'm voting to close this question as off-topic because it belongs on codereview.stackexchange.comWraparound
R0914: Too many local variables (20/15) (too-many-locals)Whitleather
If that is an answer to @JoshKarpel, I believe he wants the actual source code, not the message.Young
S
16

You are providing too little information. That said, here are some basic ideas:

  • Break out some variables into a nested function. This makes sense when you have a long function and some sections are simply producing an intermediate result.

  • Use a NamedTuple. This makes sense when you are breaking up some array into individual flags, such as database rows or pin signals.

For example:

from collections import namedtuple
Record = namedtuple('Record', 'course name id midterm1 midterm2 homework')
input_array_line = ['botony', 'chad', '123456', 88.0, 92.2, 40]
r = Record(*input_array_line)
score = (r.midterm1 + r.midterm2) * .45 + (r.homework/40.0) * 10.0
  • Use dictionaries for groups of local variables.
  • Think harder. If you have a problem that cannot be broken down and has twenty moving parts, then there is a simpler problem trying to get out.

Good luck! Keep coding! Keep notes.

Sarene answered 19/11, 2019 at 5:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.