3 ms·
Java, C# and modern PHP have a very similar style that's not at all universal. If you're used to those, Python is odd, but it can get a lot weirder. At least Py
by duckerude 8y ago
Java, C# and modern PHP have a very similar style that's not at all universal. If you're used to those, Python is odd, but it can get a lot weirder. At least Python has classes.
__init__ is called that because it initializes the object. It could have been called "__construct__", but I think "__init__" is a lot clearer about what it actually does - the object is already created when it enters __init__, __init__ just prepares it.
Calling it "self" instead of "this" is super traditional. Smalltalk did it.
The __name__ == '__main__' pattern is ugly, and I'm not a huge fan of it. You don't always need it, it's just a guard to let you tell whether a script is run directly or imported as a module. The key is that Python only has runtime. A function declaration or class declaration is a fancy variable assignment. The content of the file is executed sequentially.
Python is not especially weird, but it is different from what you're used to. Wrapping your head around it takes a while, like with all languages, but things start to click eventually.
It helps to experiment. The REPL is great for that. If you don't understand why something is the way it is, mess with it until you think you understand its behavior, then mess with it some more.