Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use underscores between words in Python function names (according to the style guide)?

The style guide says that underscores should be used, but many Python built-in functions do not. What should the criteria be for underscores? I would like to stay consistent with Python style guidelines but this area seems a little vague. Is there a good rule of thumb, is it based on my own judgment, or does it just not really matter either way?

For example, should I name my function isfoo() to match older functions, or should I name it is_foo() to match the style guideline?

like image 375
ssb Avatar asked Apr 03 '13 03:04

ssb


People also ask

What are underscores used for in Python?

It's a hint to the programmer—and it means what the Python community agrees it should mean, but it does not affect the behavior of your programs. The underscore prefix is meant as a hint to another programmer that a variable or method starting with a single underscore is intended for internal use.

What does _ and __ mean in Python?

Enforced by the Python interpreter. Double Leading and Trailing Underscore( __var__ ): Indicates special methods defined by the Python language. Avoid this naming scheme for your own attributes. Single Underscore( _ ): Sometimes used as a name for temporary or insignificant variables (“don't care”).

What do 2 underscores mean in Python?

Double underscores are used for fully private variables. According to Python documentation − If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores.

What is the purpose of __ name __ in Python?

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.


2 Answers

The style guide says that underscores should be used, but many Python built-in functions do not.

Using the built-in isinstance() as an example, this was written in 1997.

PEP 8 -- Style Guide for Python Code wasn't published until 2001, which may account for this discrepancy.

What should the criteria be for underscores? I would like to stay consistent with Python style guidelines but this area seems a little vague. Is there a good rule of thumb, is it based on my own judgment, or does it just not really matter either way?

"When in doubt, use your best judgment."

It's a style guide, not a law! Follow the guide where it is appropriate, bearing in mind the caveats (emphasis my own):

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

Therefore...

For example, should I name my function isfoo() to match older functions, or should I name it is_foo() to match the style guideline?

You should probably call it isfoo(), to follow the convention of similar functions. It is readable. "Readability counts."

like image 167
Johnsyweb Avatar answered Oct 16 '22 01:10

Johnsyweb


The style guide leaves this up to you:

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

In other words, if you feel like adding an underscore to your method name would make it easier to read -- by all means go ahead an throw one (or two!) in there. If you think that there are enough other similar cases in the standard library, then feel free to leave it out. There is no hard rule here (although others may disagree about that point). The only thing which I think is universally accepted is that you shouldn't use "CapWords" or "camelCase" for your methods. "CapWords" should be reserved for classes, and I'm not sure of any precedence for "camelCase" anywhere (though I could be wrong about that) ...

like image 36
mgilson Avatar answered Oct 16 '22 02:10

mgilson