Search code examples
pythonpython-3.xmodulenaming-conventionsnaming

How to name a Python file that consists only of a class definition?


If I have a Python file that consists only of a class definition, how should I name it?

For example, if I have this:

class Folder:
    """Represents a computer folder."""
    ...

, should I name the file folder.py or Folder.py? And what is the case if the file has (a) function definition(s) too?


Solution

  • Python classes follow the CapWords convention as you are showing in your code, then the packages and modules should be all lowercase, as per the python style guide. Packages are directories and modules are the files, so in your class case, you can name it folder.py .

    As per Paul Cornelius's comment, I'd like to make clear that these are just conventions. You are not required to follow them and they will not affect the program. Though the python.org documentation asks that python classes use CapWords and the files to be lowercase, and even has some indications about the use of underscores and other naming conventions, which are not essential to the functioning of the program.