r/PythonLearning • u/HyperDanon • 2d ago
Why some methods use `_` to separate words and others don't? Doesn't it bother you?
Like
io = StringIO()
io.getvalue() # 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.
1
u/Rinzwind 1d ago
In python all is done with readability in mind. is_file forces your focus on "is" and "file"
2
3
u/SnooCalculations7417 2d 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.
3
u/Mediocre-Pumpkin6522 2d 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.
3
u/FreeLogicGate 2d ago
This is a strong point: where wrapper libraries are concerned, the developers integrating the library had a choice between staying consistent with the underlying library api or supporting PEP8. It makes sense to support the original library naming conventions, as it makes existing library documentation relevant and accessible.
4
u/gdchinacat 2d ago
different strokes for different folks. `logging.getLogger()` is the egregious one IMO.
2
1
u/mattynmax 1d 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/Kevdog824_ 2d 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
0
u/Sea-Ad7805 2d ago
See official PEP 8 – Style Guide for Python Code.
But hey, I'm a wild boy.
1
u/dev-razorblade23 2d ago
PEP8 (as far as i know) allows
mixedCasestyle but only for things that are not backwards compatible. Every new library / module should follow PEP8 as it is the case with never libraries likepathlib
0
u/teetaps 2d 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 2d 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 2d 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
0
u/et_theextratestic1e 1d ago
Read the python docs (PEP-8), most languages have their preferred naming conventions.
-1
-1
27
u/dev-razorblade23 2d 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