I'm looking at the source code for a trie implementation
On lines 80-85:
def keys(self, prefix=[]):
return self.__keys__(prefix)
def __keys__(self, prefix=[], seen=[]):
result = []
etc.
What is def __keys__
? Is that a magic object that is self-created? If so, is this poor code? Or does __keys__
exist as a standard Python magic method? I can't find it anywhere in the Python documentation, though.
Why is it legal for the function to call self.__keys__
before def __keys__
is even instantiated? Wouldn't def __keys__
have to go before def keys
(since keys
calls __keys__
)?
The compilation of a class in Python is done way before the class is instantiated.
Whenever class type is created, the body of the class block is compiled and executed. Then, all the functions are transformed either into bound handles (normal functions) or into classmethod/staticmethod objects. Then, when a new instance is created, content of the type's __dict__
is copied over to the instance (and bound handles are transformed into methods).
Therefore, at the moment of calling instance.keys()
, the instance already has both keys
and __keys__
methods.
Also, there is no __keys__
method in any data mode, as far as I know.