6

I thought I understood self on some pretty decent level. But I came across this:

    for f in flags:
        if f not in flagMap:
            raise error.Error('bad debug flag %s' % (f,))
        self._flags = self._flags | flagMap[f]
        self('debug category \'%s\' enabled' % f)

What does self(...) do?

lukehawk
  • 1,423
  • 3
  • 22
  • 48
  • 15
    It calls `self.__call__` – Brad Solomon Feb 06 '19 at 13:48
  • 1
    Basically the `()` will call `.__call__` and this is true also for `self`. To this extent, this is more of a standard behavior than a specific `self` thing. – norok2 Feb 06 '19 at 13:50
  • 1
    Btw, there's nothing special about ``self``, it's just a variable name as any other. It's just a **convention** to use ``self`` as the first parameter in instance methods to reference the instance. – Mike Scotty Feb 06 '19 at 13:50
  • See also: https://docs.python.org/3/reference/datamodel.html#object.__call__. And for a contrived example: https://pastebin.com/nREereJX – Brad Solomon Feb 06 '19 at 13:53
  • 3
    `self` is just a variable, it is not special syntax. It depends entirely on what `self` *is* here. If it is an instance of a custom class, look for a `__call__` method. – Martijn Pieters Feb 06 '19 at 13:54

0 Answers0