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!!
So, by using a local variable you decrease the dependencies between your components, i.e. you decrease the complexity of your code. You should only use global variable when you really need to share data, but each variable should always be visible in the smallest scope possible.
It all depends on the scope of the variable. If you feel that a certain variable will take multiple values by passing through various functions then use local variables and pass them in function calls. If you feel that a certain variable you need to use will have constant value, then declare it as a global variable.
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
Good luck! Keep coding! Keep notes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With