python函数返回多个值时的数据类型是_Python3 注释多个返回值的函数类型
場景
這要是講函數注釋的用法
沒有返回值
def function(ver: str):
print(var)
單個返回值
def function(ver: str) -> dict:
a=[ver,ver,ver]
return a
多個返回值
您總是返回一個對象;使用return one, two只返回一個元組。
所以是的,-> Tuple[bool, str] 完全正確。
只有類型Tuple允許您指定元素的固定數量,每個元素都有一個不同的類型。
如果函數生成的返回值數目是固定的值,尤其是當這些值是特定的、不同的類型時,則應該始終返回元組。
from typing import Tuple
def function(ver: str) -> Tuple[str, str, bool]:
a=ver
b="BBB"
c=True
return a, b, c
A,B,C=function("hello")
print(A)
print(B)
print(C)
其他序列類型對于可變數量的元素應該有one類型規范,因此typing.Sequence在這里不合適。
另見 What's the difference between lists and tuples?
Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.
Python 的類型提示系統遵循這一原則,目前沒有語法來指定固定長度的iterable,并且在特定位置包含特定類型。
如果您必須指定任何iterable都可以,那么最好的方法是:
-> Iterable[Union[bool, str]]
此時,調用者可以期望布爾值和字符串以任意順序,長度未知(介于0和無窮大之間)。
總結
以上是生活随笔為你收集整理的python函数返回多个值时的数据类型是_Python3 注释多个返回值的函数类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个类可以实现多个接口吗_Java入门:
- 下一篇: python查看图片的源代码_pytho