Meaning of function calling in Python?

后端 未结 1 796
走了就别回头了
走了就别回头了 2021-01-03 16:02

I saw a Python function calling way in Keras\' tutorial like this:

from keras.layers import Input, Dense
from keras.models import Model

# this returns a ten         


        
相关标签:
1条回答
  • 2021-01-03 16:44

    Because Dense(...) returns a callable (basically, a function), so it can be called in turn. Here's a simple example:

    def make_adder(a):
        def the_adder(b):
            return a + b
        return the_adder
    
    add_three = make_adder(3)
    add_three(5)
    # => 8
    
    make_adder(3)(5)
    # => 8
    

    Here, make_adder(3) returns the function that is defined as

    def the_adder(b)
        return 3 + b
    

    then calling that function with argument 5 returns 8. If you skip the step of assigning the return value of make_adder(3) to a separate variable, you get the form that you were asking about: make_adder(3)(5) is the same thing as Dense(64, activation='relu')(inputs) from your question.

    EDIT: Technically, Dense is not classified as a function in Python, but as a class; Dense(...) is thus an invocation of a constructor. The class in question defines the __call__ method, which makes objects of this class "callable". Both functions and callable objects can be called by invoking them with an argument list, and the difference between the two does not impact the explanation at all. However, here's an example of a simple callable, that more closely parallels Dense:

    class Adder:
        def __init__(self, a):
            self.a = a
        def __call__(self, b):
            return self.a + b
    
    adder_of_three = Adder(3)
    adder_of_three(5)
    # => 8
    
    Adder(3)(5)
    # => 8
    
    0 讨论(0)
提交回复
热议问题