我正在编写一个模块来将 Pandas 代码转换为 SQL。我面临的问题是,我有多个仅在其他函数内部调用的函数。创建内部函数是为了使代码更具可读性并避免重复相同的代码。我应该让它们嵌套,以便代码更具可读性吗?如果它们不在其他地方使用,我是否应该始终将尽可能多的函数嵌套? “函数膨胀”呢?你们更喜欢什么?
功能是:
(1) def convert_series(self, code, library_context, code_reference)
-> 5 lines of code
-> Wraps the call of the recursive helper function (2) in a "WITH ... AS" block
-> entry-point for the user of the module
(2) def _convert_series_operations_recursive(self, node, library_context)
-> recursively translates the statement to SQL
-> 40 lines of code
-> ONLY used inside of (1)
(3) def _convert_bin_op(self, node, library_context)
-> 30 lines
-> ONLY used inside of (2)
(4) def _convert_bin_op_handle_sides(self, node, library_context)
-> 13 lines
-> ONLY used inside of (2)
我更喜欢的结构是将函数 (2)、(3)、(4) 放在 (1) 中。这会是最好的解决方案吗?另一种选择是将它们与 (1) 一起放在类中。
请记住,(2)、(3)、(4) 经常被调用,因此嵌套的唯一方法是将它们放入 (1) 中,以避免在每次递归调用中重新定义它们。