Purpose of calling function without brackets python

后端 未结 6 534
独厮守ぢ
独厮守ぢ 2020-11-27 16:07

Consider the following:

class objectTest():

    def __init__(self, a):
        self.value = a

    def get_value(self):
        return self.value

class exec         


        
6条回答
  •  死守一世寂寞
    2020-11-27 16:14

    Functions and methods in Python are also objects themselves. Thus you can compare them just as you would any other object.

    >>> type(a.get_value)
    
    >>> type(a.get_value())
    
    

    Normally of course you wouldn't compare methods to each other or anything else, because it's not terribly useful. One place it's useful is when you want to pass a function into another function.

提交回复
热议问题