Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Association between naming classes and naming their files in python (convention?)

In python (and some other languages) I have learned, that the name of a class should be written in small letters except for the first letter of each word, which should be a capital letter. Example:

class FooBar:     ... 

A class should go in a file, named the same as the class. In this example it would be a file foobar.py. If I want to import the class foo somewhere I have to do this:

from foobar import FooBar 

This convention confuses me a little. My intuition tells me, that if the filename indicates a class, than it should be written with the first letter in capitals, too, like FooBar.py. This don't look pretty in file names. Perhaps someone could tell me what is the standard convention for this?

I hope I made my question understandable. :-)

like image 656
Aufwind Avatar asked May 12 '11 13:05

Aufwind


People also ask

What is the naming convention for Python files?

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

What is a naming convention for files?

A File Naming Convention (FNC) is a framework for naming your files in a way that describes what they contain and how they relate to other files. Developing an FNC is done through identifying the key elements of the project, the important differences and commonalities between your files.

What's the spelling convention for a class name in Python?

We need to follow the camelCase convention while naming a class. You should use all lowercase while deciding a name for a method. Non Public method name should start with an underscore(_). We should use all lowercase while deciding a name for a Object.


1 Answers

What you have presented is the standard convention.

Package and Module Names

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short -- this won't be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).

Class Names

Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.

(Python Style Guide)


See e.g.

from configparser import ConfigParser 

(which, incidentally, was ConfigParser in Python 2.x but changed to be lowercase in 3.x).

like image 63
Katriel Avatar answered Oct 16 '22 01:10

Katriel