I want to order methods in a Python class, but I don't know what the correct order is.
When I extract methods in Eclipse with PyDev, Eclipse puts the extracted method on top of the modified method. But this puts the lower level details before the higher level details. According to Uncle Bob, I should do the opposite, so that my code reads like the headlines of a newspaper. When I program in Java I just follow his advice.
What is the best practice for Python?
Member variables at the top of the class, then constructors, then all other methods. And you order the methods to be close together with how they are used within the class (rather than arbitrarily putting all public then private then protected).
It doesn't matter in which order variables and functions are defined in Class in Python.
No it does not matter at all where in the class the method declaration is placed. When you actually call a method Java will go through the entire class looking for it either way, so the placement is irrelevant.
It executes the code inside a class only when it is "called", not when it is defined. So, the output should print "Out" first. But here it is printing "Middle" first. This should not happen, as python interpreter when first encounters "Middle" - it is within the definition, and thus should not be executed at that time.
As others have pointed out, there is no right way to order your methods. Maybe a PEP suggestion would be useful, but anyway. Let me try to approach your question as objectively as possible.
Interfaces first: Public methods and Python magic functions define the interface of the class. Most of the time, you and other developers want to use a class rather than change it. Thus they will be interested in the interface of that class. Putting it first in the source code avoids scrolling through implementation details you don't care about.
Properties, magic methods, public methods: It's hard to define the best order between those three, which are all part of the interface of the class. As Ethan Furman says, it's most important to stick with one system for the whole project. Generally, people expect __init__()
to the best first function in the class so I follow up with the other magic methods right below.
Reading order: Basically, there are two ways to tell a story: Bottom-up or top-down. By putting high-level functions first, a developer can get a rough understanding of the class by reading the first couple of lines. Otherwise, one would have to read the whole class in order to get any understanding of the class and most developers don't have the time for that. As a rule of thumb, put methods above all methods called from their body.
Class methods and static methods: Usually, this is implied by the reading order explained above. Normal methods can call all methods times and thus come first. Class methods can only call class methods and static methods and come next. Static methods cannot call other methods of the class and come last.
Most of these rules are not Python-specific by the way. I'm not aware of a language that enforces method order.
There is no one correct order. Pick a system and stick with it. The one I use is:
class SomeClass(object): def __magic_methods__(self): "magic methods first, usually in alphabetical order" def _private_method(self): "worker methods next, also in alphabetical order" def a_method(self): "then normal methods, also in alphabetical order"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With