Why NOT only one class per file?
Klaas
mike.klaas at gmail.com
Wed Apr 4 18:38:55 EDT 2007
On Apr 4, 2:52 pm, Thomas Krüger <newsgro... at nospam.nowire.org> wrote:
>
> At first: if he really like it he can place every class in a single
> file. But there are some reasons why Python "allows" you to place many
> classes in one file:
>
> - It's (a little bit) faster, no additional file system lookup is needed. ;)
> - You can define a class in a class. Django, for example, uses this for
> it's data models. If you do this you are forced to have multiple classes
> in on file. Example:http://www.djangoproject.com/documentation/tutorial02/#make-the-poll-...
That is somewhat specious: inner classes can be defined in java too.
The main reason is that in java, classes are magical entities which
correspond to one "exportable" unit of code. Thus it makes a great
deal of sense to limit to one _public_ class per file (java also
allows unlimited private and package-private classes defined in a
single file).
If you want to define a bunch of utility functions in java, you write
a file containing a single class with static methods.
In python, classes do not have special status. The exportable unit of
code is a module, which, like public classes in java, can contain
functions, static variables, and classes. Similar to java, you are
limited to a single module object per file (modulo extreme trickery).
If you want to define a bunch of utility functions in python, you
write a file containing a single module with functions.
-Mike
More information about the Python-list
mailing list