1

I'm not quite understand what this code mean:

# instantiate the input layer
x = Input(batch_shape=batch_input_shape,
          dtype=input_dtype, name=name)
# this will build the current layer
# and create the node connecting the current layer
# to the input layer we just created.
self(x)

This code is inside a member function of a class, please refer to https://github.com/fchollet/keras/blob/master/keras/engine/topology.py line 341. When I step into the self(x), it jumps to another member function __call__ of this class. Why does this happen? Thanks.

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
tuming1990
  • 89
  • 3
  • 8
  • 4
    https://docs.python.org/3/reference/datamodel.html#object.__call__ – interjay Jun 02 '16 at 16:27
  • An object can be callable if its class has a `__call__` method. `self` is the current object. When you call an object then control is passed to `__call__` - as you found. You can test if this is supported by using the built-in function `callable()`. – cdarke Jun 02 '16 at 16:28
  • @cdarke Thanks for your explanation. I understand now. – tuming1990 Jun 02 '16 at 16:30

1 Answers1

0

__call__() is one of the special methods in Python. It allows classes to emulate function types.

self is probably --as by convention-- the first argument to your member function, i.e. the instance of the object the method is called on.

self() thus uses the current instance object as a function. By definition, this means that the __call__() method on the object will be invoked, which is exactly what happens.

dhke
  • 15,008
  • 2
  • 39
  • 56