[英]Python: how to check if an argument's type is such that it will be passed by value or by reference?
[英]Is there a way to check each passed argument's type in Python?
假設我有一個接受任意數量參數的函數。 除了循環,有沒有辦法檢查每個參數是否是一個列表?
def func(*args):
# check if all args are lists
我正在嘗試any()
和all()
方法,但不知道是否有辦法將isinstance()
和type()
與它們一起使用。
肯定的事:
all_lists = all(isinstance(arg, list) for arg in args)
all(isinstance(arg, list) for arg in args)
當然,生成器表達式中隱藏了一個循環; 在一天結束時,必須有某種循環..
我喜歡上面的答案:
all_lists = all(isinstance(arg, list) for arg in args)
但請注意,您還可以在較新版本的 Python 中進行類型提示:
def func(*args:list):
# check if all args are lists
你可以從Mypy
類的工具中獲得一些類型檢查
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.