How to import only one module in a package when the package __init__.py has already imports the modules?

Peng Yu pengyu.ut at gmail.com
Sat Oct 31 19:29:35 EDT 2009


On Sat, Oct 31, 2009 at 5:45 PM, Wolodja Wentland
<wentland at cl.uni-heidelberg.de> wrote:
> On Sat, Oct 31, 2009 at 16:53 -0500, Peng Yu wrote:
>> On Sat, Oct 31, 2009 at 4:14 PM, Robert Kern <robert.kern at gmail.com> wrote:
>
> [ snip ]
>
>> I know that multiple classes or functions are typically defined in one
>> file (i.e. module in python). However, I feel this make the code not
>> easy to read. Therefore, I insist on one class or function per file
>> (i.e module in python).
>
> Are you serious? Do you *really* put each function in its own file? How
> exactly does this enhance the readability of the source code? Especially
> if you compare that to a (sic!) modularisation scheme that groups
> classes and functions together by task or semantic relatedness.

If two functions are too long to put in file, I generally put them in
two different files. And I always put a single class in a file.
Suppose that I have many functions in one file, it is not clear to see
how many functions are in the file at first glance. If I put each
function in its own file,  just by looking at the directory structure,
I can easily see how many functions there are.

One advantage is on refactoring. When each function has its own file,
I can change variable names, etc., for a give function without
worrying accidentally change variable names in other functions. When I
find a function is more appropriate to put in another namespace, I can
just move the file around.

Another advantage is on testing. I can have associated test dir for
each class or function. By this way I can easily locate the definition
and the test to track any potential problems.

You can use package rather than module to group semantic related
classes and functions.


> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
>
> iQIcBAEBCAAGBQJK7L38AAoJEIt/fTDK8U78Pv4P/icEwsmSLcuINWZHj4Shs0A/
> 1/TZcuP5VwuR6gZ0WsywKzbKJqL0WDahUi4o3VkwFuM2mKskXzch3buZ5NvlwOp6
> I+NqBn9jCTb3nXiVb5wHdF6uYf84BPZe1WccRIDJLIoGRWX/V6tmMH1LtLGnBeVi
> RGOd6Mz2KGr1cgisyYJ2h4Qm5tzKNuZ1KDtzXoOG4DYzwEEZBITFOwDNXy5tihJz
> v/NcAZOa4aBfJZtKxA7Ikl+30nDV8ZZhEU7Br/rIus2JrSqMp6gAh4f+zTz9jQzL
> Sp7O3bTQiHoghej+G4YW+/eMDTiNDSKm1u8++V5svwednp/mmYBnzA8aIPKFSoN6
> vn4D0Q2XVGQAoWyY7pT9zyRKBJnn63xXD5h9T6JimEz7uMWGzTebIuxFRHsd1vkt
> TYTW1kJDH8aIsy51egBezZx8o6sntBFu3D+D3itqDW2D2sZ75sPiblgkLCWHvZMR
> RaBjCkvhVjOaiJOZ64mRmkW3RUJzY6lGvEqfQqW1bRpHxLEUKaWLy6rWa0sQTTut
> rIZ/5TdnjPec1Dx+9v6V7sW8bZtCttpb7j4k+DBAMjRpW7mocnGfuxGN/57Y/uD0
> gFOURpMz1rjEdPCiYZuUQX+joS3tl9IxnBZL7gTRl3slSWoVlGuhcqsew3nAkrGB
> Fx8iwKUAwwRULxzigHHB
> =n3s3
> -----END PGP SIGNATURE-----
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



More information about the Python-list mailing list