Camelcase in Python

This article explains camelcase in Python.

What is camelcase?

Camelcase refers to a style of writing where each word in the middle of a phrase is written with a capital latter. The intention with this style is to help a reader identify and read individual words even when they are written together.

Look at this example, tostring. At first glance this might not make much sense. Upon further observation you will notice that it’s actually two words written together “to” and “string. Now if you wrote this in Camelcase, it would be toString. Now personally this makes alot more sense. In cases with three or more words Camelcase becomes more and more useful.


What does PEP say?

PEP stands for Python Enhancement Proposal. PEP is not a compulsory set of rules that everyone must follow, rather it’s a set of guidelines that Python programmers can choose to follow to maintain consistency between python programs developed by different people. Basically it aims to be the Universal standard for all Python programmers.

This is what PEP officially has to say regarding the matter.

Function names should be lowercase, with words separated by underscores as necessary to improve readability. Variable names follow the same convention as function names. mixedCase is allowed only in contexts where that’s already the prevailing style.

Basically the PEP way is to write a variable/function name like this, to_string. This is also pretty readable as the words are separated by the underscore, making things pretty clear. Since all the characters are meant to be in lowercase, there won’t be any confusions either.

Bonus Fact: If you use a IDE like PyCharm, it will automatically keep making suggestions and ensure that you follow the PEP protocol.


Conclusion

In the end, it doesn’t make too much of a difference whatever you pick. What’s more important is that you remain consistent. If you keep switching styles halfway through your code, people will get confused. Pick a style, and stick to it. Consistency is key.

There are several scenarios in which you can (highly recommended) change your style. David Goodger (in “Code Like a Pythonista” here) describes the PEP 8 recommendations as follows:

  • joined_lower for functions, methods, attributes, variables
  • joined_lower or ALL_CAPS for constants
  • StudlyCaps for classes
  • camelCase only to conform to pre-existing conventions

As mentioned above, it’s pretty common to have all caps to represent constants. And usually we don’t use underscores when naming classes, so use a variation of camelcase with it. The only difference is that unlike camelcase, the first letter is also capital.

Follow these conventions and you’ll be one step closer to mastering Python (or any other language really).


This marks the end of the Python Camelcase article. Any suggestions or Contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments