5

Can someone explain the reasoning for why we can't use init instead __init__? I'm learning and this is breaking my brain.

Comments
  • 6
    Because, python.

    def init() would be so much nicer though.
  • 0
    PHP ("magic methods") and Javascript also have this. Open your browser console and type Array.__lookupGetter__ or Object.__proto__. It is a naming convention used in multiple languages for methods that can (or should) only be executed directly from inside the object/class instance itself. By prefixing them with a double underscore, it avoids naming clashes with methods a developer would define.
  • 0
    Because they wanted to reserve init() for user defined functions (see the flask documentation).
  • 0
    That's just the way it is. Python looks ugly
  • 1
    Because in python, functions and variables that you aren't supposed to call directly, but rather using operators or language features, are named like that.

    __eq__
    __getattr__
    __repr__

    They are pretty consistent about that. Just the way it is. It prevents it from clashing if you accidently make an init function and reduces the list of forbidden keywords.
  • 1
    @beegC0de s/ugly/beautiful
Add Comment