3

I am very new to python and am uncertain about a few things.

  1. Should classes be in individual files?
  2. Does a standalone script have a main method? If so, how does this work as I have a main method which isnt getting called
  3. Where does the unit test directory normally get placed and how does it 'include' the files it is testing?
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • Number 1 is sorta personal preference, unlike Java where i think its required, in python it isnt. BUT from what I've seen most people put all of their classes in one file, just for import simplicity. i would rather import one thing and have `import theClasses` than `import class1,class2,class3`. – TehTris May 31 '13 at 21:48
  • Number 2 is whatever you want it to be. Generally people will put `if __name__ == "__main__": go() ` (or whatever their main function is called ) at the end of their python file and that will cause it to run `go()` as soon as its double clicked or loaded from command prompt – TehTris May 31 '13 at 21:50

3 Answers3

5

Lot of ground to cover here, you might be better served by splitting these points up into individual questions / queries : )

  1. I'd resist splitting classes into individual files unnecessarily. Some classes may depend heavily on others, and I (personally) like to keep them in the same file. I feel that the best convention is to create files based on functionality, rather than a separate file for each class.

    I've taken a lot of inspiration by work from Pocoo projects like Flask and Jinja2. The Jinja2 source provides an excellent example of "bundling" certain related classes within files: Github

  2. Standalone scripts typically use something like if __name__ == '__main__', to check if is executed directly (e.g. python scriptname.py), and then execute a function typically main() afterwards (thanks Mike for correcting my mistake).

    To better assist in resolving your issue of main not being called, i'd suggest showing the code your are trying to use.

  3. There are many different ways to do this, but the convention seems to be in a tests, test, testsuite directory inside your project. The latter part of this question gets quite tricky, because Python imports vary depending on the structure/type of your Python project (is it a Python package? a standalone collection of scripts?) and your PYTHONPATH.

    This SO post is a wonderful place to start in regards to considering where to put your tests, although the general answer seems to be "wherever is best for you/your team" : ).

    On a slightly related note, I'd also suggest looking into the Nose testing framework, as it removes a lot of the boilerplate from testing with Python.

Community
  • 1
  • 1
Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
2

I will answer questions 1 and 2.

1) This depends. If your classes are small then it is OK to put them in the same file. If they get big they are usually kept in separate files.
2) No, there does not need to be a main method. However, for a script, this is a mighty common idiom:

def main():
    # Your code

# At the bottom of the file
if __name__ == '__main__':
    main()

This way, your main will only get run when the file is run as a script, but it won't be run when imported as a module. Python will just execute any code it encounters not in a function when you import the module, so this protects you if you want to reuse a script as a module later.

This is just an idiom, main has no special meaning in python so you don't have to call it or anything.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
0
  1. Does not have to, but if the class is used by several other files/classes it should.
  2. It is a good practice see What does <if __name__==“__main__”:> do?
  3. Python looks at $PYTHONPATH which is a list of directories to find modules in. You can put modules in subdirectories of one of these directories and import them with import directoryname.module.. You can import a class by from directoryname.module import ClassName.
Community
  • 1
  • 1
cforbish
  • 8,567
  • 3
  • 28
  • 32