[Chicago] Python style question: class constants

Thomas Johnson thomas.j.johnson at gmail.com
Thu Mar 6 18:36:36 CET 2014


I posted this question at
http://codereview.stackexchange.com/questions/43619/proper-use-of-class-constantsbut
the people there apparently have fairly narrow definitions of
"on-topic". I'm hoping someone here can lend some insight.

Basically I have a bunch of classes like this scattered around different
modules in my project:

class MyObject:
    ABOVE = 1
    BELOW = 2

    def do_stuff(self, direction):
        if direction == self.ABOVE:
            # Do above stuff
        elif direction == self.BELOW:
            # Do below stuff


If I have a bunch of different classes in different modules that each use
their own ABOVE and BELOW, what's the best way to handle it?

Should I extract these constants to their own class, maybe in its own
module? Would creating a constants.py module be overkill?

Should I keep the class constants within the classes, since ABOVE and BELOW
might mean slightly different things in different classes?

Is there a better / more pythonic solution to this design issue?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20140306/e66b6a79/attachment.html>


More information about the Chicago mailing list