![](/img/trans.png)
[英]How can I write a Python function that will only accept a list with int or float type inside?
[英]How can I accept only some strings in Python function?
我想讓它像這樣:
>>> myfunc("strawberry")
ok
# myfunc only works with strawberry
我知道大多數人會回答:
def myfunc(something):
if something == "strawberry":
print("ok")
但我想在參數設置中完成這一切。
就像,有點像這樣:
def myfunc(something: OnlyThese["strawberry", "cake"]:
print("ok")
雖然上面的代碼很不正確,但是我想看看Python是否已經有這樣的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.