簡體   English   中英

我可以不在我的代碼中使用列表並且仍然得到相同的結果嗎?

[英]Can I not use a list in my code and still get the same result?

我只是想知道我是否能夠不使用下面代碼中的列表並且仍然得到相同的結果:

colors = random.choice(['blue' , 'red' , 'green'])

print(colors)

沒有列表就無法調用 function。

如果您查看文檔... https://docs.python.org/3/library/random.html#random.choice

...它說明選擇 function 接受單個參數seq

這表明參數需要是一個序列。

我們可以通過查看 typeshed 接口文件 - https://github.com/python/typeshed/blob/dd73f117f04306b0ccf840aa497fc89a0d49c083/stdlib/random.pyi#L18進一步深入研究

這些描述了 function 接受和返回的數據類型。

這解釋了 function 需要一個Sequence 序列描述了索引對象的集合(基本上是一個列表)。

如果random.choice的簽名看起來像這樣(注意args之前的* )......

def choice(*args):
    ...

...那么您就不會將參數作為列表提供。

看到這篇文章... https://realpython.com/python-kwargs-and-args/#using-the-python-args-variable-in-function-definitions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM