What is the default for naming modules (fonts) in Python?

Asked

Viewed 55 times

9

In Java we have the custom of creating fonts with the first uppercase letter for example: Carrinho.java and DetalheCompraFragment.java.

But so far I have not found a standard for naming modules in Python.

I recently saw files with these formats: detalhe_compra.py, detalhecompra.py, DetalheCompra.py, DETALHECOMPRA.py.

That said, there is doubt: In Python there is a default or the developer chooses how to name?

1 answer

9


Yes. Python has several style guides called PEP (Python Enhancement Proposals). In particular, PEP 8 brings details on how to correctly name each type of identifier.

Reads:

Modules should have short, all-lowercase Names. Underscores can be used in the module name if it improves readability.

Or, in free translation:

Modules should have short names and in lower case. Underscores (_) can be used in the module name if this improves readability.

Therefore, detalhecompra.py and detalhe_compra.py are the two names allowed by the Style Guide.

Classes

For classes, however, the rule is different. The same document brings the following:

Class Names should normally use the Capwords Convention.

The naming Convention for functions may be used Instead in cases Where the interface is documented and used primarily as a callable.

Or, in free translation:

Class names should normally use the Capwords convention (or Pascalcase).

The naming convention for functions can be used in cases where the interface is documented and used primarily as a callable.

So it’s common to have a file detalhe_compra.py containing the class DetalheCompra. The import gets like this:

from detalhe_compra import DetalheCompra

This, of course, also happens in stdlibs:

from typing import NewType
  • 1

    Héliton, thank you so much for the answer, but I’m still having a question. This same pattern applies when we’re talking about classes?

  • 1

    @Guilhermebigois, I added the part about classes :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.