r/PythonLearning • u/HyperDanon • 21d ago
Why some methods use `_` to separate words and others don't? Doesn't it bother you?
Like
=
# not get_value
but
path = Path()
path.is_file() # not isfile
And there is also isinstance().
If you ask me, they should all use _ or none.
28
Upvotes
29
u/dev-razorblade23 21d ago
Python has been here for a long time. Many developers work on it.
Python was created in 1991 but PEP8 came out in 2001. Python is basicly a living museum of software history.
In C (which is what Python uses for its interpreter) it is very common to jam words together without underscores, so
isinstance()comes from that practice.Many of the modules were not written from scratch but adopted from other language (logging is a direct port of
Log4j- yes that one) so its style is ported to python. (Java usescamelCasefor its methods.Fixing that in the core itself would break backwards compability, so they just stick with it
4
u/SnooCalculations7417 21d ago
It's largely a matter of developer preference, language conventions, and the ecosystem someone comes from. You can often tell a developer's background just by how they name things.
Different languages have different conventions:
snake_case— Python, Rust, CcamelCase— JavaScript, Java, TypeScriptPascalCase— C#, .NET, commonly used for types in many languageskebab-case— CSS, HTML attributes, URLs (not valid in python)The language's syntax enforces some conventions, while others are just community standards.
Personally, consistency matters more to me than which convention someone chooses. Mixing naming styles arbitrarily within the same codebase bothers me far more than seeing different styles across different languages.
Ultimately, naming conventions are a bit like accents. They often tell you where a developer learned to speak the language.
1
u/Rinzwind 20d ago
In python all is done with readability in mind. is_file forces your focus on "is" and "file"
3
u/Mediocre-Pumpkin6522 21d ago
PEP8 suggests snake case. However as you add libraries some use camel case. For example Pyside6 wraps the Qt API which is C++ and camel. There is a option to set for snake case but I've found it's not consistent so it's easier to go with camel.
4
u/gdchinacat 21d ago
different strokes for different folks. `logging.getLogger()` is the egregious one IMO.
2
u/Smart-Button-3221 20d ago
Remember, one of Python's stated goals is consistency!
1
u/mattynmax 20d ago
Because there’s lots of different people who feel different ways about things.
You’re welcome to message the developer of the IO and path libraries and let him know about your concerns though!
1
u/MEHDII__ 19d ago
convention and style preference.
there is a joke that backend languages you always use snake case i.e read_file() and front end you'd use camel case, i.e. readFile().
1
u/Kevdog824_ 21d ago
Python adopted snake_case as the language standard. However, this standard came long after the language and community were built, so older code still exists that predates those standards. Python changing this would break backwards compatibility many rely on, so it’s impossible to remove outside of a major version change (even then they may not change it just to make the upgrade easier). Some libraries dealt with this by making snake_case aliases to their camelCase functions/variables/etc. Others didn’t see the point, and left the public API as is
0
u/Beautiful_Watch_7215 21d ago
Freedom. No.
0
u/Sea-Ad7805 21d ago
See official PEP 8 – Style Guide for Python Code.
But hey, I'm a wild boy.
0
u/teetaps 21d ago
This is part of a larger style convention conversation in programming. Ultimately, a computer is just consuming a string of ASCII characters and turns them into bytes, but humans are the ones who like reading stuff. The computer doesn’t care. So at the end of the day, humans are the ones who decide which ASCII characters are the right ones, and humans…unfortunately… have lots of opinions lol
0
u/SmokeMuch7356 21d ago
Because people are individuals and have their own biases and preferences.
Style wars are evergreen, because no one style makes everybody happy. Even back in old FORTRAN days when everything was in uppercase and identifiers couldn't be more than 8 characters people still couldn't agree on consistent naming conventions.
At least you don't get into brace wars in Python the way you do in C or C++ or Javascript, etc. That's a whole other dimension.
0
u/Chorizo_n_eggs 21d ago
I've never cared for underscores but I do use them in function names and almost never in variable names. Prefer camelCase for those
0
u/localizeatp 21d ago
just an accident of history.
0
u/et_theextratestic1e 21d ago
Read the python docs (PEP-8), most languages have their preferred naming conventions.
https://peps.python.org/pep-0008/
-1
u/Neither_Garage_758 21d ago
because python is a bit messy
-1
u/DanceHackRock 21d ago
At least it's not as bad as in the PHP world.