简体   繁体   English

将函数输出分配给许多变量:好的或坏的做法

[英]Assigning function output to many variables: good or bad practice

Is it an unusual design pattern to assign output of function to many variables?将函数的输出分配给许多变量是一种不寻常的设计模式吗? Imagine each variable below representing complex data, such as nested dictionaries.想象一下下面的每个变量都代表复杂的数据,例如嵌套字典。

def step_1():
    a = ...
    ...
    i = ...

    return a, b, c, d, e, f, g, h, i

def step_2(a, b, c, d, e, f):
    ...
    return j
    
def step_3(g, h, i, j):
    ...
    return k


a, b, c, d, e, f, g, h, i = step_1()
j = step_2(a, b, c, d, e, f)
k = step_3(g, h, i, j)
# do something with k

Somehow it feels odd doing things like不知何故,做一些事情感觉很奇怪

a, b, c, d, e, f, g, h, i = step_1() or a, b, c, d, e, f, g, h, i = step_1()

return a, b, c, d, e, f, g, h, i or return a, b, c, d, e, f, g, h, i

j = step_2(a, b, c, d, e, f) . j = step_2(a, b, c, d, e, f)

Generally in such cases a seperate dictionary is maintained with all the outputs being stored it has many issue.通常在这种情况下,维护一个单独的字典,其中存储所有输出,它有很多问题。 you can save it as json easily or even you can pass required fields from it manually but this makes the redability of the code much more.您可以轻松地将其保存为 json,甚至可以手动传递所需的字段,但这使得代码的可重复性更高。

For example here final_output = {"a":a,'b':b,'c':c}例如这里final_output = {"a":a,'b':b,'c':c}

and returning this final_output instead.并返回这个final_output This actually I use it in for business purposes.这实际上我将它用于商业目的。

If you're returning a,b,c,...,i , that's a bit excessive, though in general this isn't (necessarily) a poor design choice.如果您要返回a,b,c,...,i ,那就有点过分了,尽管总的来说这不是(必然)一个糟糕的设计选择。 In reality, this is the product of two Python features:实际上,这是两个 Python 特性的产物:

  • From the return statement, Python is interpreting/ treating the return type as a tuple that, syntactically, has been expanded, rather than treating it as the union of however many variables individually.return语句中,Python 将返回类型解释/处理为一个元组,从语法上讲,它已被扩展,而不是将其视为单个变量的联合。 That is, you could equivalently define a tuple tuple = (a,...,i) and return that.也就是说,您可以等效地定义一个元组tuple = (a,...,i)并返回它。
  • Then again when you assign the a, ..., i = step1() , you're again expanding this tuple.然后,当您再次分配a, ..., i = step1()时,您再次扩展了这个元组。

What you might consider is treating it as a tuple, ie, a single object, rather than 9 individual items, since it doesn't seem that you need all of them at once.您可能会考虑将其视为一个元组,即单个对象,而不是 9 个单独的项目,因为您似乎不需要一次全部使用它们。 This might be better for code style, etc.这对于代码风格等可能更好。

Since you make the distinction that these a,...,i are "complex data" items, though you have given little about your particular case, it's probably best to split up this functionality to make its scope as clear, tight, and therefore manageable as possible.由于您将这些a,...,i分为“复杂数据”项,尽管您对您的特定情况几乎没有给出任何说明,因此最好拆分此功能以使其范围清晰、紧凑,因此尽可能管理。 If I was working with you, I'd probably implore you to do so before I'd write any unit tests for it.如果我和你一起工作,我可能会在为它编写任何单元测试之前恳求你这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM