Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Variable Naming vs C++ Variable Naming

Tags:

python

I am starting to get deeper in learning Python and having a great time doing it but there is one thing that is bothering me. Why is it in C++ answers/tutorial/books are variables namedLikeThis and in Python they are named_like_this?

Is this just personal preference or is it a convention that should be followed for readability/clarity sake? Not a big deal but I don't want to be the weird guy writing annoying looking code.

like image 911
kylieCatt Avatar asked May 23 '26 16:05

kylieCatt


2 Answers

People writing Python generally follow PEP8 (http://www.python.org/dev/peps/pep-0008/).

like image 60
thebjorn Avatar answered May 25 '26 07:05

thebjorn


To expand on @thebjorn's answer, from PEP8:

Package and Module Names

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

...

Class Names

Class names should normally use the CapWords convention.

...

Function Names

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.

...

Method Names and Instance Variables

Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.

...

Constants

Constants are usually defined on a module level and written in all capital letters with underscores separating words.


I definitely recommend reading PEP8 in its entirety, not only the naming conventions part. You'll be a much better Pythonista for it :)

like image 36
MattDMo Avatar answered May 25 '26 07:05

MattDMo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!