Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to order methods in a Python class?

Tags:

python

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?

like image 979
zetafish Avatar asked Apr 23 '12 23:04

zetafish


People also ask

How do you order methods in class?

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).

Does the order of methods in a class matter Python?

It doesn't matter in which order variables and functions are defined in Class in Python.

Does order of methods in a class matter?

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.

What is correct order of execution about class and object in Python?

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.


2 Answers

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.

like image 77
danijar Avatar answered Oct 21 '22 15:10

danijar


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" 
like image 30
Ethan Furman Avatar answered Oct 21 '22 16:10

Ethan Furman