Purpose of calling function without brackets python

后端 未结 6 550
独厮守ぢ
独厮守ぢ 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:27

    Difference between function without parentheses and with parentheses is that when using parentheses you will get the output of that function and when you use the function without parentheses you create a copy of that function. for example

    def outerFunction(text): 
        text = text 
    
        def innerFunction(): 
            print(text) 
    
        return innerFunction()
    
    if __name__ == '__main__': 
        outerFunction('Hey!') 
        x = outerFunction
        y = x 
        x('Hey i am busy can you call me later')
        y('this is not a function')
    

    here we copy the function outerFunction to x and then copy y to x.

提交回复
热议问题