Should __init__ always be the first method of a class?
Posted by Faskill@reddit | Python | View on Reddit | 14 comments
I'm just wondering if I'm being unreasonable or if it's something that other people like to do also. I'm a senior DE for context.
I always define functions in the order they are called, no exceptions. I feel like this make the code much easier to read.
Now a junior created a class with an init function that includes 6-7 methods, and those methods are at the bottom of the class definition, which makes the class difficult to read in my opinion. Is it unreasonable to ask him to put these methods before the __init__ method or am I the only one to actually do that? :)
BraindeadCelery@reddit
I would rather have the init at the top bc. that is common practice and where i would look for these things when browsing the codebase.
If the other methods are well named, it should also be clear what they are doing.
AiutoIlLupo@reddit
In general, it's bad practice to have an init that calls method of the same class, unless they are themselves sub initialisation methods. In that case, I generally put them after the init, but I set a divider between the subinit methods and the rest of the methods. I also make them all underscored, of course.
Aka:
Python is not C that you must define earlier or declare. In this case, putting them earlier gives no context when you are reading top to bottom. Why are these methods here at the top? oooh.. they are needed by init, vs: ok, here is init... and these are methods that init needs.
timwaaagh@reddit
the order they are called is this: init then functions called from init. so first init then those methods. putting the methods before init just makes it worse. putting them just after init is the correct way, according to Clean Code. Clean Code is somewhat controversial in other aspects but this just makes sense imo.
Natural-Intelligence@reddit
If you know the order of the methods being called, are you sure functions are not the right tool for the task? It sounds like you may be writing quite functional things.
But I would stick to the Python conventions than forcing the junior having your own personal preferences (which certainly is minority). Most Python developers write init method first (or new if there is such).
Also, writing the methods in the order of they are called is also opinionated. Some prefer grouping them logically or writing them from low level to high level. Maybe not force this if the junior has a sane order. Navigating the methods with proper IDE should not be an issue.
Anru_Kitakaze@reddit
Junior actually used correct order. Init first, then init will call those methods. I'm not sure either OP did mistake explaining their own rule, or... I don't have any other option actually. Junior did exactly what OP want.
Put closer? Yeah, why not?
Put above? No way
bmag147@reddit
I think some people here might be mis-understanding your question. Correct me if I'm wrong, but from reading it I'm assuming the class looks something like this:
and you'd like it to look like this:
If so, I agree with you.
M1KE234@reddit
If the init function calls those methods then surely the unit function is called first and should be at the top like usual?
Anru_Kitakaze@reddit
If you define a functions/methods in the order of execution, I don't understand why you want to put methods, which ARE CALLED BY init to the top?
__new__
, then__init__
, then everything else. It's YOUR rule.And yes, it makes more sense to put init at the top. Because it's dunder AND called in the beginning.
Why do you want to go against your rule and put it down?
divad1196@reddit
The position of a method alone doesn't matter. Especially not about "the call order" which most of the time irrelevant.
You can do logical grouping (e.g. all special method together, all getter/setter, everything related to feature X, ...) for your conveniance, it helps putting meaningful comments on a portion of the code. But that's it.
Key-Ice-8638@reddit
In my humble opinion, I put my init method first, and any initialization methods come immediately after. It just helps me group together initialization logic.
I should also mention that if it's not an init method, the sub-methods will be above it (there is no particular reason for this distinction, but it looks pretty to me)
MajorSloe@reddit
I feel like it makes the most sense for the init to be after the class declaration (unless e.g. new is being defined) since that is the code which is run right as an instance is created. Instance methods are run after the instance is already created.
So if you prefer to define functions in the order they are called, then you would actually put the init before the methods (i.e. at the top). What you are suggesting (putting the init at the bottom) is actually the other way around.
saint_geser@reddit
I don't understand the question: if you always define methods and functions in the order they're called then unless you define
__new__
then__init__
will always be the first method of a class to be called.ThatSituation9908@reddit
I don't read the code top to bottom, I follow the call stack.
Yolt0123@reddit
I get what you're saying, but with most modern editors, the code shows itself as you move down the function highlighting calls. I find that the code doesn't need to be close if the functions are well named - but I like my code to read like a story...