KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to refactor a big Python function into smaller ones. For example, consider this following code snippet: x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope variables that would have to be passed to the extracted function, which could look like: def mysum(x1, x2, x3, x4, x5, x6, x7, x8, x9): x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 return x The problem is that Pylint would trigger a warning about too many arguments. I could avoid the warning by doing something like: def mysum(d): x1 = d['x1'] x2 = d['x2'] ... x9 = d['x9'] x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 return x def mybigfunction(): ... d = {} d['x1'] = x1 ... d['x9'] = x9 x = mysum(d) but this approach loos ugly to me. It requires writing a lot of code that is even redundant. Is there a better way to do it?
Tags (comma-separated)
Save Edits
Cancel