12

I'm learning Python and I have been playing around with packages. I wanted to know the best way to define classes in packages. It seems that the only way to define classes in a package is to define them in the __init__.py of that package. Coming from Java, I'd kind of like to define individual files for my classes. Is this a recommended practice?

I'd like to have my directory look somewhat like this:

recursor/
 __init__.py
 RecursionException.py
 RecursionResult.py
 Recursor.py

So I could refer to my classes as recursor.Recursor, recursor.RecursionException, and recursor.RecursionResult. Is this doable or recommended in Python?

martineau
124k29 gold badges181 silver badges319 bronze badges
asked Jun 11, 2010 at 16:28
3
  • 4
    Obligatory: Python is not Java Commented Jun 11, 2010 at 17:09
  • Also check out stackoverflow.com/questions/2339371/… Commented Jun 11, 2010 at 17:16
  • 2
    Yes you can, but no you shouldn't. Commented Jun 11, 2010 at 19:02

3 Answers 3

12

Go ahead and define your classes in separate modules. Then make __init__.py do something like this:

from RecursionException import RecursionException
from RecursionResult import RecursionResult
from Recursor import Recursor

That will import each class into the package's root namespace, so calling code can refer to recursor.Recursor instead of recursor.Recursor.Recursor.

I feel the need to echo some of the other comments here, though: Python is not Java. Rather than creating a new module for every class under the sun, I suggest grouping closely related classes into a single module. It's easier to understand your code that way, and calling code won't need a bazillion imports.

answered Jun 11, 2010 at 18:59
Sign up to request clarification or add additional context in comments.

1 Comment

Although recursor.Recursor.Recursor does have certain charm to it.
2

This is perfectly doable. Just create a new class module for each of those classes, and create exactly the structure you posted.

You can also make a Recursion.py module or something similar, and include all 3 classes in that file.

(I'm also new to Python from Java, and I haven't yet put anything in my __init__.py files...)

answered Jun 11, 2010 at 16:30

2 Comments

If I create the directory layout above, defining my Recursor class in the Recursor.py file and I try to import "recursor.Recursor," I get an ImportError. What am I doing wrong?
@rfkrocktk - you may have to specify recursor.Recursor.Recursor... the first for the package, the second for the module (the file), and the third for the actual class... (It's sometimes better to consider using modules sort of as packages... instead of a package recursor with these 3 classes as separate files, you can have a module recursor with these 3 classes inside it)
1

In Python you're not restricted to defining 1 class per file and few do that. You can if you want to though - it's totally up to you. A Package in Python is just a directory with an

__init__.py 

file. You don't have to put anything in that file you can to control what gets imported etc.

answered Jun 11, 2010 at 16:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.