简体   繁体   English

以结构化方式从python中的函数返回多个值的替代方法?

[英]Alternative to return multiple values from function in python in a structured way?

I'm working on mortgage calculations and many functions calculate multiple different values that I need to return.我正在研究抵押贷款计算,许多函数计算我需要返回的多个不同值。

For example:例如:

def calculate_something(incomes, debts):
    ...
    return {'value':150000, 'extra':{'dsti_gross':450, 'dsti_supergross':480}}

has many intermediate calculations that are useful in other calculations.有许多在其他计算中有用的中间计算。 These functions are not divisible into smaller functions as it would make the code slower.这些函数不能分成更小的函数,因为它会使代码变慢。

I don't want to return tuples as the code might change in the future and I'd need to check all the function calls.我不想返回元组,因为代码将来可能会改变,我需要检查所有的函数调用。

Dictionary is now a way to go but dictionary value calls aren't statically analyzed (most IDEs) so you always need to check the function if you access the correct keys.字典现在是一种方法,但字典值调用不是静态分析的(大多数 IDE),所以如果你访问正确的键,你总是需要检查函数。

Is there another way to work with such returns?还有其他方法可以处理此类回报吗? Considering classes/objects but every function returns different keys.考虑类/对象,但每个函数返回不同的键。

要扩展使用类和子类的建议,您可以利用python dataclasses ,它们几乎是为此类问题设计的。

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

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