Magic methods and order of definition

I am looking for source code for trie implementation

In lines 80-85:

def keys(self, prefix=[]): return self.__keys__(prefix) def __keys__(self, prefix=[], seen=[]): result = [] etc. 
  • What is def __keys__ ? Is this magical object self-created? If so, is this bad code? Or does __keys__ exist as a standard Python magic method? However, I cannot find it anywhere in the Python documentation.

  • Why is it legal for a function to call self.__keys__ before def __keys__ even instantiated? Would there be def __keys__ before def keys (since keys calls __keys__ )?

+4
source share
5 answers

Compiling a class in Python is done before the class is instantiated.

Whenever a class type is created, the body of the class block is compiled and executed. Then, all functions are converted either to linked handles (regular functions) or to classmethod / staticmethod objects. Then, when a new instance is created, content of type __dict__ copied to the instance (and the associated handles are converted to methods).

Therefore, at the time instance.keys() called, the instance.keys() already has the keys and __keys__ .

Also, in any data mode, there is no __keys__ method as far as I know.

-one
source

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.

+2
source

All real "magic methods" are in the documentation for the data model ; __keys__ not one of them. The manual says:

Never invent such names; use them only as documented.

so yes, compiling a new one is a bad form (an agreement would be to call it _keys ).

The second part of your question does not make sense; even if it was not a class, there is no need to define methods and functions in the order in which they are called. As long as they exist at the time of the call, this is not a problem. I am inclined to define public methods before private ones, although the former may name the latter, just for the reader’s convenience.

+2
source
  • There is no magic method called __keys__() , as you suspected it was just a bad name.

  • The code in the class definition can be in any order. All questions that were made by the determination at the time of the actual call are performed downstream.

0
source

There is no magic method called __keys__ , so its just a wrong naming convention. Looking at the code, the author simply wanted to have a private method that is used internally as well as from the public keys method. As you can see, __keys__ accepts an additional argument.

On the second question, there is no need to define functions in the same order in which they were called. It will be available by timecode.

0
source

All Articles