Python Code / Function Schema

I am learning Python and trying to find a better way to structure my code.

Let's say that I have a long function and you want to break it down into smaller functions. In C I would make it a β€œstatic” function at the top level (since this is the only level of functions). I will also probably redirect it to declare and place it after the already shortened function that uses it.

Now for Python. In Python, I have the ability to create a nested function. Since this new β€œinternal” function is actually just part of a larger function, interrupted for readability purposes and used only by it, it looks like it should be a nested function, but this function inside the parent function calls the whole function still to be very long, so no code was really removed from it! And especially because functions must be fully encoded before they are called, this means that the actual short function is completely omitted at the end of this pseudo-random function, which makes readability awful!

What is considered good practice for such situations?

+4
source share
4 answers

How to place small functions in your own file and import them into your main function? You will have something like:

 def main_func(): from impl import a, b, c a() b() c() 

I think this approach leads to high readability: you see where there are fewer functions, if you want to learn them by importing them into one liner, and the implementation of the main function is directly visible. By choosing the appropriate file name / location, you can also tell the user that these functions are not intended to be used outside main_func (you still do not hide the information in Python).

By the way: this question does not have one correct answer.

+2
source

As far as I know, the main advantage of internal functions in Python is that they inherit the volume of the closing function. Therefore, if you need access to variables in the scope of the main function (for example, an argument or a local variable), the inner function is the way to go. Otherwise, do what you like and / or find it most readable.

EDIT: See this answer .

+2
source

So, I realized that you have a long function like:

 def long_func(blah, foo, *args): ... ... my_val = long_func(foo, blah, a, b, c) 

What have you done:

 def long_func(blah, foo, *args): def short_func1(): ... def short_func2(): ... ... short_func1() short_func2() ... ... my_val = long_func(foo, blah, a, b, c) 

You have many more options, I will list two:

  • Do it in class

     class SomeName(object): def __init__(self, blah, foo, *args): self.blah = blah self.foo = foo self.args = args self.result = None # Might keep this for returning values or see (2) def short_func1(self): ... def short_func2(self): ... def run(self): # name it as you like! self.short_func1() self.short_func2() return self.result # (2) or return the last call, on you ... my_val = SomeName(foo, blah, a, b, c).run() 
  • Make another module and put short_funcs in it. Just as flyx suggested.

     def long_func(foo, blah, *args): from my_module import short_func1, short_func2 short_func1(foo) short_func2(blah) 
+2
source

It is good practice to preserve the cycomatic complex . This practically means breaking your long function into many smaller functions.

Complexity is measured by the number of if, while, do, for,?:, Catch, switch, case statements and the operators && & and || (plus one) to the constructor body, method, static initializer, or instance initializer. This is an indicator of the minimum number of possible paths through the source and, therefore, the number of tests required. Usually 1-4 is considered good, 5-7 ok, 8-10 consider refactoring, and 11+ re-factor now!

I suggest using this advice based on Sonar, a code quality analysis tool. A good way to reorganize such code is to use TDD. First write unit tests to cover all the paths for your current function. After that, you can reorganize with peace of mind that unit tests ensure that you don't break anything.

If, on the other hand, your long function is simply long, but otherwise already has low cyclic complexity, then I think it doesn't really matter if the function is nested or not.

0
source

Source: https://habr.com/ru/post/1499312/


All Articles