3

For example:

from urllib.request import urlopen, Request

is there any rule about the capitalization of the first letter?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Huanyu
  • 39
  • 3
  • 1
    Close-voters: Whether there is or isn't a rule regarding capitalization is *not* a matter of opinion. – Sneftel Jul 09 '18 at 10:38
  • 4
    `Request` is a class, `urlopen` is a function. https://www.python.org/dev/peps/pep-0008/#naming-conventions – Håken Lid Jul 09 '18 at 10:38
  • To expand upon @HåkenLid comment, classes are usually named using the PascalCase (or CamelCase) convention while functions are all lower cased separated with underscores. – devius Jul 09 '18 at 10:42
  • 1
    Possible duplicate of [What is the naming convention in Python for variable and function names?](https://stackoverflow.com/questions/159720/what-is-the-naming-convention-in-python-for-variable-and-function-names) – devius Jul 09 '18 at 10:44

2 Answers2

1

Most of the answer is in pep08 (python coding style guidelines) - basically, classes are spelled in CamelCase, everything else in lower_with_underscores.

But note that (for various reasons), this convention is not always respected in Python itself (builtins and stdlib):

  • quite a few classes are named in all_lower, notably the datetime, date, time and timedelta from the datetime package, all of the builtin types (type, property, int, float, str, list, tuple, dict, set, object etc)

  • the logging package uses mixedCase for most functions and methods (ie logging.getLogger() which should be logging.get_logger() etc)

For the builtin types, there's at least an historical reason: most of those names where initially bound to functions, not to classes (I'm talking python 1.5.x or even older). For what it's worth, quite a few of them are actually still documented as being functions when they are not...

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
0

The best practices in python is to use small letter in the name of functions but, when dealing with classes we capitalize the first letter.