What does __ * (any word) __ or _ * mean in Python?

8

Reading a book from time to time it puts __init__ or __init and I do not know what those "_" means, I tried to search the internet for some explanations, but I ended up making it harder, could someone help me explaining more practical way?

    
asked by anonymous 16.12.2017 / 11:14

1 answer

8

Gambiarra:)

These things were inserted into the language too late, as these identifiers could already be being used by existing Python codes needed to do something not to break the existing codes and like __ is "reserved" resolved the issue.

These identifiers are for "magic" members of the language, so it is not a common name, it is something the language treats in a special way and generates something specific.

__init__ for example is a method that will initialize the object, what you write there will be executed every time an object is instantiated. The language will call it for you. So it is not any method, its presence in your code tells the language what it should do.

The official explanation is that this is to differentiate that it is a "magic" function, but it is not consistent.

    
16.12.2017 / 11:22