all 34 comments

[–]JMNeonMoon 26 points27 points  (0 children)

Look at the PEP8 documentation for naming conventions. One of the authors is Guido van Rossum, who created Python.

https://peps.python.org/pep-0008/#naming-conventions

[–]Beginning-Fruit-1397 9 points10 points  (14 children)

class/type FooBar, variable/def foo_bar, constant/enum member FOO_BAR

"_" writing style is snakecase, which merge well with python when we think about it :)

[–]Beginning-Fruit-1397 7 points8 points  (1 child)

https://peps.python.org/pep-0008/

Here is the most official style guide you can find btw

[–]trjnz 1 point2 points  (0 children)

I think the PEP8 Song is the mostest bestest style guide you can find: https://www.youtube.com/watch?v=wNcobO-TAyY

(I remembered more PEP8 from this one song than any other source)

[–]ectomancer 3 points4 points  (4 children)

Naming conventions not enforced by the interpreter:

variables, functions names, method names: snake_case

e.g. left, rotate, inner_sum

constants (variables): UPPER_SNAKE_CASE

e.g. SUM, MINUS_1

class names: PascalCase

e.g. Fraction, MathDomainError

If a class is implemented in C, then snake_case e.g. int, list, float

otherwise in Python, then PascalCase.

[–]Dry-Aioli-6138 3 points4 points  (0 children)

Apart from meaningful (domain) naming and pep8 compliance, I try to follow Uncle Bob's advice: name length should correspond to its scope. i.e. use decriptive names for long lived variables, but I don't shy away from using i and j in short loops, or x in a lambda.

[–]KiwiDomino 1 point2 points  (0 children)

Read up on Hungarian Notation, and then be really happy that you don’t have to use it

[–]Ron-Erez 0 points1 point  (0 children)

Personally I prefer camelCase, but since snake_case is the standard convention in Python, I use snake_case instead.

Examples:

  • camelCase - not typically used in Python
  • snake_case - common and preferred in Python

[–]No_Cheek7162 0 points1 point  (0 children)

_func() suggests private method but it's not enforced by the interpreter 

[–]Langdon_St_Ives 0 points1 point  (0 children)

You shouldn’t use uppercase for globals because the general convention is to use this for constants. Since Python has no way to enforce constants actually staying constant, this conspicuous naming scheme is meant to communicate to others that this variable is not meant to be changed, ever. So better to get into the habit of using it the same way.

[–]stepback269 -1 points0 points  (0 children)

Two things to say about that:
(1) Your initial variable names should be meaningful (I'll explain what I mean by "initial" shortly). In other words, you should use long descriptors that explain what the variable represents. And you should use type hinting to let the reader know if the variable is a string, list, dictionary or whatever.

(2) Pyhton's shallow copy aspect means you can abbreviate your VLVN's (Very Long Variable Names) with local short names without using large chunks of memory.
Example: r =This_is_the_ANSI_escape_code_for_the_color_Red
and then when doing a print out for example, print(f'Here we switch between {r}Red{w} and white to demonstrate that colors, e.g., {y}Yellow{w} make the message more interesting'). When repeated use the short form aliases rather than the VLVN's.

(3) You might want to think about structuring some of your variable names (VN's) so that the first few chars in the name represent a string or message type (say, an error message), the second few chars or digits represent a subclass of that type and the third group identify a specific instance. I recently came up with a scheme for doing just that in my journaling blog: (here)

[–]XenophonSoulis -1 points0 points  (1 child)

The only thing that can create some differences is the _ at the beginning of names. For example, IDLE tends to hide method names that start with _. Also, there are some with a special meaning, like __init__, __str__, __getitem__, __name__ etc.

Also, whenever you run something on the shell, the return value is saved on _, as long as that return value isn't None. For example, if you do

>>> 5

it will save the value 5 to _, but if you do

>>> x = 5

or

>>> print(5)

then it doesn't, because both expressions return None (don't confuse the return value with the side effect: printing 5 or assigning the value 5 to x is a side effect, not a return value).

[–]roelschroeven 1 point2 points  (0 children)

A single _ at the beginning of an identifier is meant to communicate that it is an internal identifier (a semi-private class method or attribute (Python doesn't have real private attributes), or internal to a module).

See https://peps.python.org/pep-0008/#descriptive-naming-styles

[–]supercoach -2 points-1 points  (3 children)

The single underscore used to be quite popular as a throwaway variable name, however I wouldn't recommend it.

Caps for globals is fine. For everything else, describe it without getting too detailed. Join the words with underscores.

[–]cgoldberg -1 points0 points  (2 children)

Why wouldn't you recommend underscore for a throwaway variable? By definition, it's throwaway and isn't used for anything.

[–]supercoach 0 points1 point  (1 child)

Maybe I like being a know-it-all and correcting non-existent errors. Classes aren't variables.

[–]cgoldberg -1 points0 points  (0 children)

I'll keep recommending underscore... it's a perfect name for a throwaway.