[Tutor] using separate py files for classes

Alan Gauld alan.gauld at btinternet.com
Wed Nov 2 11:13:21 CET 2011


On 02/11/11 06:29, Chris Hare wrote:
>
> I would like to put each of my classes in separate files to make it
> easier to edit them and keep the various files as small as possible for
> editing purposes.
>
> I have come across a couple of problems:
>
> 1. I have to use import statements like "from file import class" instead
> of "import file"
> 2. I have to include all of the import statements for things not found
> in the class file
>
> Is this normal or am I doing something wrong?

That's exactly what happens. Its actually a good thing since it makes 
your code more reliable, although it may not seem like it while you are 
developing it! :-)

You might like to bend your rules of one class per file though. Its 
usually more convenient to have groups of related classes in a single file.

Especially if there is a dependency involved since otherwise the using 
class code winds up with lots of module references in  it. And if you 
come to distribute the code you have to always distribute both files not 
one. And its easy to forget the dependency exists over time...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list