Python3中内置函数callable介绍

x33g5p2x  于2021-12-26 转载在 Python  
字(2.0k)|赞(0)|评价(0)|浏览(225)

**      Python3中的内置函数callable接受一个对象参数,如果此对象参数看起来可调用,则callable函数返回True,否则返回False**。如果返回True,则调用仍有可能失败;但如果返回False,则调用对象将永远不会成功。

**     **类是可调用的(调用类会返回一个新实例),如果它们的类具有__call__方法,则实例是可调用的。

**      可以使用()运算符调用的任何(****或任何对象)**都称为可调用对象

**     **在Python3中,一切都是对象,如函数、方法甚至类。

**     **所有内置函数都是可调用的。

**     **所有用户定义的函数也是可调用的。

**     **lambda函数也是可调用的。

**     **用户定义的类也是可调用的。

**     **所有内置方法也是可调用的,如list类包含insert,remove等内置方法,同样,int, float等也都有自己的方法。

**   **  以下为测试代码:

var = 2
if var == 1:
    # reference: https://www.geeksforgeeks.org/callable-in-python/
    def Geek():
        return 5
  
    # an object is created of Geek()
    let = Geek; print(callable(let)) # True
    let2 = Geek(); print(callable(let2)) # False
  
    # a test variable
    num = 5 * 5; print(callable(num)) # False

    class Geek2:
        def __call__(self):
            print('Hello GeeksforGeeks 2')
  
    # Suggests that the Geek2 class is callable
    print(callable(Geek2)) # True
  
    # This proves that class is callable
    GeekObject2 = Geek2()
    # an instance of a class with a __call__ method
    GeekObject2() # this is calling the __call__ method, Hello GeeksforGeeks 2

    class Geek3:
        def testFunc(self):
            print('Hello GeeksforGeeks 3')
  
    # Suggests that the Geek3 class is callable
    print(callable(Geek3)) # True
  
    # the Geek3 class is callable, but the instance of Geek3 is not callable() and it returns a runtime error
    GeekObject3 = Geek3()
    # The object will be created but returns an error on calling
    GeekObject3() # TypeError: 'Geek3' object is not callable
elif var == 2:
    # reference: https://pythonsimplified.com/what-is-a-callable-in-python/
    # 内置函数也是函数,因此它们也是可调用的,即callable()返回True. 所有的内置函数都是可调用的
    print(callable(min)) # True
    print(callable(max)) # True
    print(callable(print)) # True
    print(callable(int)) # True, int class
    print(callable(float)) # True, float class

    print(callable(str)) # True, str class
    print(callable(list)) # True, list class

    # 所有用户定义的函数也是可调用的
    def hello(user):
        print(f"Welcome to Python Simplified, {user}")

    print(callable(hello)) # True

    # lambda函数也是可调用的
    print(callable(lambda x: x**2)) # True

    # 用户定义的类也是可调用的
    class Rectangle:
        def __init__(self, width, height):
            self.height = height
            self.width = width
        def area(self):
            return self.width * self.area

    print(callable(Rectangle)) # True

    # 所有内置方法也是可调用的
    my_list = [1,2,3,4,5]
    print(callable(my_list.append)) # True

print("test finish")

    GitHubhttp://github.com/fengbingchun/Python_Test

相关文章