![](/img/trans.png)
[英]Python, how to pass arbitrary variables from function to function in the most elegant way?
[英]Elegant way to return lot of variables from a function
我有一個 class 的 python 方法,它正在計算一堆東西,將它們存儲在 8 個不同的變量中,然后想要返回這些值。
線上的東西;
def rate_lookup(self, a):
....
....
return(charge,
handling_charge,
delivery_charge,
fuel_surcharge,
overheight_surcharge,
security_charge,
documentation_fee,
unpacking_removal_fee)
問題是我必須將這些返回值保存在 function 調用上的另一組類似變量中。 這看起來不是很優雅,並且使用了很多變量。
我確實需要每個變量值,因為我需要稍后根據某些標准將它們打印到控制台。
什么是重新調整大量變量值的最佳方法。
IMO,這通常意味着您的 function做得太多,您可能想將其分解為幾個功能或 Class。 如果您仍然決定要使用單個function ,我建議使用命名元組以您可以按名稱引用它們的方式返回值。
你需要一個數據類。 選擇一個最適合您的:
__slots__
的定制 class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.