For your second question, this is legal, the functions for the class are defined when the class is defined, so you can be sure that both functions will be defined before keys() is called, the logic also applies to normal functions, we can do -
>>> def a(): ... b() ... >>> def b(): ... print("In B()") ... >>> a() In B()
This is legal because both a() and b() defined before calling a() . This would be illegal if you try to call a() before the value of b() is determined. Note that the function definition does not automatically call it, and python does not check during function definition, regardless of whether the functions used in the function are defined or not (before execution, when the function is called, in which case it throws a NameError)
For your first question, I do not know such magic methods called __keys__() , nor can I find it in the documentation.
source share