Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is the proper or standard way to layout a class. If a class where to have attributes, methods, private attributes, private methods, class property (methods), and class methods. I am done 99% of my coding procedurely and beyond working with ORM haven't focused to much on OOP. I am changing that in a current project and was just curios what the standard way of organizing your classes was.

Class MyClass():
   attr1 = value1
   attr2 = value2
   _attr3 = value3
   def __init__(self):
      do some stuff
   def do_something(self):
      do some more stuff

   def __do_something_else(self):
      more stuff
   def attr4(self):
       return do_some_stuff_else()

   def generator_a_bunch_of_these(cls):
share|improve this question
Have a look at PEP8 - The Python Style Guide – Luke Graham Jun 6 '11 at 13:46
Note that double leading underscores are generally a bad idea. You can't even use them in subclasses, must less from somewhere else (yes, that's sometimes justified), because the name is mangled. – delnan Jun 6 '11 at 13:58
Your use of class-level attributes (attr1, attr2 and _attr3) is often a very bad idea. Folks who come from languages with static declarations are sometimes confused by this and think that these class-level attributes somehow important. Also, 80% of the time, you want @staticmethod not @classmethod. – S.Lott Jun 6 '11 at 14:46
@Omnius: What parts of the built-in library have you read? What other open-source libraries do you use normally? Have you read that code to see how it's formatted? – S.Lott Jun 6 '11 at 18:59
up vote 2 down vote accepted

Look for such answers in here:

Basics is in the most famous one:

By the looks and code i have seen, you got pretty good idea about that. Good luck, hope it'll help :)

UPDATE: While following pep rules you come up with about the same anyway :) so pure convention for classes - yes there is none or i haven't heard of it. How you should write it - pep8. By common programming sense you write you variables, fields above methods. Same things happen with everything else. Just code as the Holy Zen of Python teaches you:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
share|improve this answer
I have read through these documents prior to my question and did find a wealth of good information however none that addressed the question of how the class should be physically laid out. Perhaps a convention doesn't exist and I just need to make some rules for our office on how we want to do it. – Ominus Jun 6 '11 at 13:53
I've just updated my answer, o thought to reply a comment but then i realized that its more of an update and clarification. – JackLeo Jun 6 '11 at 14:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.