如何檢測 Python 變數是否為函式?
在本文中,我們將學習如何檢測 Python 變數是否為函式。
有時,弄清楚 Python 變數是否為函式至關重要。當代碼長達一千行,而你又不是程式碼的建立者時,這似乎沒什麼價值,你可能會質疑一個變數是否是函式。
使用的方法
以下是檢查 python 變數是否為函式的方法:
使用內建的 callable() 函式
使用 inspect 模組的 isfunction()
使用 type() 函式
使用內建的 hasattr() 函式
使用 isinstance() 函式
方法 1:使用內建的 callable() 函式
callable() 函式返回布林值結果。如果函式可呼叫,則返回 True,否則返回 False。
語法
callable(object)
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立任何隨機函式。此函式返回傳遞給它的兩個數字的和。
使用 return 關鍵字返回傳遞的兩個數字的和。
使用 callable() 函式檢查傳遞的物件(即 addition)是否為函式。如果它是函式,則返回 True,否則返回 False。
建立一個變數來儲存輸入數字。
同樣,使用 callable() 函式檢查變數 'number' 是否為函式。
示例
以下程式使用內建的 callable() 函式檢查 python 變數是否為函式:
# creating a function that returns the addition
# of 2 numbers passed to it
def addition(p, q):
# returning the sum of the given two numbers(arguments)
return p+q
# using the callable() function to check whether
# the variable 'addition' is a function
# it returns True if it is a function else False
print(callable(addition))
number = 10
# checking whether the variable 'number' is a function
print(callable(number))
輸出
執行後,上述程式將生成以下輸出:
True False
方法 2:使用 inspect 模組的 isfunction()
inspect 模組的 isfunction() 函式可用於確定變數是否為函式。如果它是函式,則返回布林值 True,否則返回 False。
此外,要使用它,必須首先從 inspect 模組匯入 isfunction,然後使用它才能獲得布林值。
示例
以下程式使用 inspect 模組的 isfunction() 函式檢查 python 變數是否為函式:
# importing isfunction from inspect module
from inspect import isfunction
# creating a function that returns the addition
# of 2 numbers passed to it
def addition(p, q):
# returning the sum of the given two numbers(arguments)
return p+q
# using the isfunction() function to check whether
# the variable 'addition' is a function or not
# it returns True if it is a function else False
print(isfunction(addition))
number = 10
print(isfunction(number))
輸出
執行後,上述程式將生成以下輸出:
True False
方法 3:使用 type() 函式
type() 函式識別物件的型別,以便我們可以根據物件是否是函式型別來確定它是否可呼叫。
簡單來說,type() 函式返回物件的 資料型別。
示例
以下程式使用 type() 函式檢查 python 變數是否為函式:
# creating a function that returns the addition
# of 2 numbers passed to it
def addition(p, q):
# returning the sum of the given two numbers(arguments)
return p+q
# checking the type of the variable by passing
# the variable as an argument to it
print(type(addition))
# given Variable
number = 10
print(type(number))
輸出
執行後,上述程式將生成以下輸出:
<class 'function'> <class 'int'>
方法 4:使用內建的 hasattr() 函式
hasattr() 是一個函式,它識別物件的型別,以便我們可以確定該物件型別是否為函式。與 callable() 一樣,它也返回布林值。
示例
以下程式使用內建的 hasattr() 函式檢查 python 變數是否為函式:
# creating a function that returns the addition # of 2 numbers passed to it def addition(p, q): # returning the sum of the given two numbers(arguments) return p+q # checking whether the type of variable 'addition' # is a function or not using hasattr() function # it returns True if it is a function else False print(hasattr(addition, '__call__')) number = 10 # checking whether the variable 'number' is a function or not print(hasattr(number, '__call__'))
輸出
執行後,上述程式將生成以下輸出:
True False
方法 5:使用 isinstance() 函式
isinstance() 是一個函式,它識別物件的型別,以便我們可以確定該物件是否為函式。它返回布林值。
示例
以下程式使用 isinstance() 函式檢查 python 變數是否為函式:
# importing the types module import types # creating a function that returns the addition # of 2 numbers passed to it def addition(p, q): # # returning the sum of the given two numbers(arguments) return p+q # passing object, types.FunctionType as an argument to the # isinstance() function to check whether it is a function or not print(isinstance(addition, types.FunctionType)) number = 10 # checking whether the above variable 'number' is a function or not print(isinstance(number, types.FunctionType))
輸出
執行後,上述程式將生成以下輸出:
True False
結論
本文介紹了五種不同的方法來確定輸入變數是否是函式型別。我們還熟悉了 hasattr() 和 isinstance() 函式,它們允許我們確定兩個變數是否為相同型別。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP