首頁 > 後端開發 > Python教學 > 如何偵測一個Python變數是否為函數?

如何偵測一個Python變數是否為函數?

WBOY
發布: 2023-08-26 14:05:03
轉載
1363 人瀏覽過

如何偵測一個Python變數是否為函數?

在本文中,我們將學習如何偵測一個Python變數是否為函數。

有時候,要確定一個Python變數是否是一個函數是很重要的。當程式碼有上千行並且你不是程式碼的創建者時,這可能看起來毫無價值,你可能會質疑一個變數是否是一個函數。

Methods Used

以下是檢查Python變數是否為函數的方法:

  • 使用內建的callable()函數

  • 使用 inspect 模組的 isfunction() 方法

  • 使用type()函數

  • #使用內建的 hasattr() 函數

  • #使用isinstance()函數

Method 1: Using the built-in callable() function

The callable() function returns a boolean result. It returns True if the function is callable else it returns False.

Syntax

callable(object)
登入後複製

演算法(步驟)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • 建立任意隨機函數。這裡的函數傳回傳遞給它的兩個數字的相加結果。

  • 使用 return 關鍵字傳回傳入的兩個數字的和。

  • Use the callable() function to check whether the object passed ( i.e, addition) is a function or NOT. It returns True if it is a function else False.

  • #建立一個變數來儲存輸入的數字。

  • Similarly, check whether the variable 'number' is a function or not using the callable() function.

#Example

The following program checks whether a python variable is a function or not using the built-in callable() function −

# 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。

Additionally, to utilize this, you must first import isfunction from inspect module before using it to obtain a boolean value.

Example

以下程式使用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
登入後複製
登入後複製
登入後複製
登入後複製

Method 3: Using the type() function

The type() function identifies the type of an object so that we may determine whether it is callable or not based on whether the object is of the function type.

In simple terms, the type() function returns the data type of an object.

Example

以下程式使用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'>
登入後複製

Method 4: Using the built-in hasattr() function

The hasattr() is a function that identifies the type of an object so that we can determine whether or not that object type is a function. In the same way as callable(), it also returns a boolean value.

Example

The following program checks whether a python variable is a function or not using the built-in hasattr() function −

# 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()函數

The isinstance() is a function that identifies the type of an object so that we may determine whether or not that object is a function. It returns a boolean value.

Example

The following program checks whether a python variable is a function or not using isinstance() function −

#
# 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
登入後複製
登入後複製
登入後複製
登入後複製

Conclusion

這篇文章教會了我們五種不同的方法來確定輸入變數是否為函數類型。我們也熟悉了hasattr()和isinstance()函數,它們可以幫助我們確定兩個變數是否屬於相同的類型。

以上是如何偵測一個Python變數是否為函數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板