Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
naming conventions
#1
I'm somewhat new to Python. I see there is a convention to use CamelCase for classes, UPPER_CASE for constants, and snake_case for pretty much everything else. But I see many names, even in the standard library, that just run words together. For example, Python 3.9 adds the string methods removeprefix and removesuffix. Is there some guideline that leads to that convention? Seems like it would be hard to guess when underscores will be used and when they will be omitted.
Reply
#2
I guess there are some personal presence here,and also that only one of exciting string methods use snake_case.
I personally in this case would have probably gone for this.
remove_suffix
remove_prefix
The one is.
>>> s = 'hello'
>>> s.format_map
<built-in method format_map of str object at 0x000002C7C5C0E3F0>
The rest without special method's __something__.
>>> dir(s)[33:]
['capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isascii',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']
Reply
#3
I guess the argument for 'removeprefix' was consistency with 'expandtabs' or 'splitlines' or 'startswith'. It is a special case for the 'str' class due to method names that date back to python's early history. Note that pep 8 was only created in july 2001, ten years after Python's first release.
Reply
#4
Does most newer code (after 2001) using snake_case?
Maybe the pattern to look for is just consistency within a class.
It sure would be nice if the snake_case convention was followed everywhere.
Reply
#5
Pep 8 was written for the core Python developers, so I suppose they tried to follow these rules as much as possible, but obviously there are exceptions. A large part of the Python community adopted Pep 8. I think most of the useful code that you can find uses this convention.

Sometimes people adopt a different convention because they are porting to Python an interface that already exists in C or in Java and they choose the same structures and functions names to keep pace with existing code and documentation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  naming entities(city abbreviations) tirumalaramakrishna 1 1,198 May-06-2022, 11:22 AM
Last Post: jefsummers
  Python Style and Naming Variables rsherry8 3 2,170 Jun-07-2021, 09:30 PM
Last Post: deanhystad
  Naming the file as time and date. BettyTurnips 3 2,890 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Question about naming variables in class methods sShadowSerpent 1 1,963 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Dyanmically Naming Files ovidius 3 2,489 Jan-29-2020, 02:44 PM
Last Post: buran
  naming images adding to number within multiple functions Bmart6969 0 1,894 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Sub: Python-3: Better Avoid Naming A Variable As list ? adt 9 3,938 Aug-29-2019, 08:15 AM
Last Post: adt
  Naming convention advice Alfalfa 5 3,280 Jul-21-2018, 11:47 AM
Last Post: Larz60+
  Python Naming Error: List not defined Intelligent_Agent0 1 14,272 Mar-13-2018, 08:34 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020