從Python中的函數返回多個值的最佳方法是什麼? (What's the best way to return multiple values from a function in Python?)


問題描述

從函數返回多個值的最佳方法是什麼? (What's the best way to return multiple values from a function?)

我有一個函數,我需要對字符串做一些事情。我需要該函數返回一個指示操作是否成功的布爾值,並且我還需要返回修改後的字符串。

在 C# 中,我會為字符串使用 out 參數,但在 Python 中沒有等效參數。我對 Python 還是很陌生,我唯一能想到的就是返回一個帶有布爾值和修改後的字符串的元組。

相關問題:函數返回多個值是pythonic嗎?


參考解法

方法 1:

def f(in_str):
out_str = in_str.upper()
return True, out_str # Creates tuple automatically

succeeded, b = f("a") # Automatic tuple unpacking
</code></pre>

方法 2:

Why not throw an exception if the operation wasn't successful? Personally, I tend to be of the opinion that if you need to return more than one value from a function, you should reconsider if you're doing things the right way or use an object.

But more directly to the point, if you throw an exception, you're forcing them to deal with the problem. If you try to return a value that indicates failure, it's very well possible somebody could not check the value and end up with some potentially hard to debug errors.

方法 3:

Return a tuple.

def f(x):
    # do stuff
    return (True, modified_string)

success, modified_string = f(something)

方法 4:

Returning a tuple is the usual way to do this in Python.

方法 5:

Throwing an exception for failure is one good way to proceed, and if you're returning a lot of different values, you can return a tuple. For the specific case you're citing, I often take an intermediate approach: return the modified string on success, and return None on failure. I'm enough of an unreconstructed C programmer to want to return a NULL pointer to char on failure.

If I were writing a routine to be used as part of a larger library and consumed by other developers, I'd throw an exception on failure. When I'm eating my own dogfood, I'll probably return different types and test on return.

(by John RutherfordJohn MillikinJason BakerrmmhChris Upchurchtuxedo)

參考文件

  1. What's the best way to return multiple values from a function? (CC BY‑SA 2.5/3.0/4.0)

#return #Variables #Python






相關問題

如果評估的最後一個語句是 If 語句,Ruby 會返回什麼 (What is Returned in Ruby if the Last Statement Evaluated is an If Statement)

將多個參數傳遞給 javascript 並根據兩個參數 + php 循環更新 html 內容 (pass multiple parameters to a javascript and update html content bases on both parameters + php loop)

Loại Không khớp không thể chuyển đổi từ Boolean sang Int (Mảng trong phương thức tùy chỉnh) (Type Mismatch can't convert from Boolean to Int (Arrays in custom method))

C++返回一個帶有私有構造函數的類實例 (C++ return a class instance with private constructor)

ArrayList.isEmpty() 時如何返回值? (How do I return a value when ArrayList.isEmpty()?)

如何將坐標(元組)格式化為字符串? (How can I format a coordinate (tuple) as a string?)

void 類型方法表達式體成員允許非 void 類型的表達式 *如果其他方法* (Void-type method expression-bodied member allows expression of non-void type *if other method*)

paypal動態退貨地址 (paypal dynamic return address)

讓兩個類進行交互 (getting two classes to interact)

在 C++ 中返回“this”? (return "this" in C++?)

Java:枚舉的不同返回類型 (Java: Different return type by enum)

如何檢查函數參數和類型 (How to inspect function arguments and types)







留言討論