4

The PEP 8 style guide (Python) says methodnames should be lowercase and that sometimes method_names may have embedded underscores. Do experienced Python programmers often come across CamelCase (with leading capital) methods?

H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
  • 1
    CamelCase (leading capital) for **methods**? That's madness. Not even Java/C# do this (perhaps you meant mixedCase?). –  Dec 30 '10 at 08:33
  • 2
    @delnan: I believe that many C# conventions prescribe PascalCase or "CamelCase with leading capitals" (a.k.a., with tongue-in-cheek, "Bactrian Camel" case) for public methods. Sun's coding conventions for Java, on the other hand, do indeed prescribe camelCase without leading captials (true camelCase, a.k.a., "Dromedarian camel" Case) for methods. – Jeet Dec 30 '10 at 09:00
  • 1
    @delnan: In C#/.NET, almost (?) all public symbols begin with a capital letter, including Classes, Methods, Constants and Enumerations. – Ferdinand Beyer Dec 30 '10 at 09:33
  • 1
    @Jeet @Ferdinant Nevermind the C# bit then, I stand corrected. –  Dec 30 '10 at 09:38
  • 1
    Answer: "No, not often, but it happens." Couldn't you have figured that out? – Lennart Regebro Dec 30 '10 at 10:46
  • Writing camelCase is harmful. Read [PEP 8](http://www.python.org/dev/peps/pep-0008/) and [about `logging`](http://stackoverflow.com/questions/22993667/how-come-the-pythons-logging-module-doesnt-follow-pep8-conventions). – 0 _ Jan 18 '15 at 19:03

6 Answers6

7

In my experience, a module's naming conventions depends on the following factors:

  • If it's a newly written, Python-only module, or if the authors especially care for "Pythonic code" (quite common!), the naming usually sticks to the recommendations in the Python Style Guide.
  • Beginners to Python often stick to their favorite style, especially if they have a Java/C#/... background.
  • Some companies prefer a consistent naming style across languages.
  • Some Python modules adapt an API from other languages (especially Java in the threading and xml.dom modules), including naming style.
  • Python bindings for C/C++ libraries usually keep the original naming (e.g. camelCase for PyQt or PascalCase for VTK)

I think the last point is the most common way that you will find PascalCase in Python code.

Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
  • I like @6502's link to "Style is Substance" but I have to pick an answer and this is a good one. – H2ONaCl Dec 30 '10 at 07:44
3

I was notorious for camel-casing in Python, having just prior come from Java. No more of that, though. I much prefer the "method_name" style of capitalization.

One Python library that I know of, that for sure has camel-cased method names, is the xml.dom package, and its subpackages (like xml.dom.minidom).

nesv
  • 786
  • 6
  • 10
1

By my experience, yes, some libraries use camelCase, however is not the most common one.

I personally encourage to follow the python standard (as defined in the PEP008), the reason is very simple: Every person with a different programming language background may tend to "impose" its own code style and that is pretty dangerous, imagine a java fan and a php fan writting python code together... could be fun.

However, if there is already a proposed standard, why not follow it? i mean in java everyone uses camelCase, why? well simply because it is the convention.

Finally you have cool tools as pylint that are configured by default to check for the PEP008 syntax.

Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
1

I found myself using camelCase for method names quite a bit (I use underscored_names for attributes). The main reason I got this habit is probably that I've been using Qt.

Probably it would have been simpler if Python forced a naming convention and four space indenting ... style is substance.

6502
  • 112,025
  • 15
  • 165
  • 265
  • 2
    Python already scares away people with its "forced conventions", like indentation. It would probably lose even more users (like me) if spaces were required. There are, however, some other languages that enforce capitalization. Haskell is an example of such a language. – pafcu Dec 30 '10 at 07:28
  • 4
    When starting with python I was crazy enough to use a two-spaces indent style. How stupid. I had to have a smart editor to corrupt my program by reindenting it to realize how idiotic was to cling to a non standard convention for no real reason. While that "What? Significant spaces!? Are they serious?" feeling is probably of everyone starts with Python after learning say C/C++/Java it's also true that I never, ever found a smart programmer that after a while didn't tell me that Python syntax is indeed great. You'd probably push away someone... not smart ones however. – 6502 Dec 30 '10 at 07:57
  • 3
    People are different, and different people prefer different styles. I prefer to look at "beautiful" code, but just like in art, beauty is in the eye of the beholder. How many people criticize art that uses a non-standard style? Many. Does that mean that they are correct? No. Personally I like Python's syntax, but I'm not as arrogant as to suggest that anyone who doesn't is "not smart". – pafcu Dec 30 '10 at 08:47
  • 2
    pafcu, I really like your comments on this answer. Although I really like Python and its syntax, I guess many people will agree that its strict rules prevent some features and uses that other languages can offer. For example, ruby-style blocks (anonymous functions / multi-statement lambdas) are pretty much irreconcilable with Python's indentation / no end-statement policy. – Ferdinand Beyer Dec 30 '10 at 09:07
1

I personally prefer not to use camelCase in Python (or at all -- my other daily use language is C), but I've seen it done. I had to do it once because we were using a mixed codebase (some Python and a lot of previously-written Matlab code) and the Matlab code used camelCase, though I'm not sure why since Matlab seems to frown on camel case as well.

donkey_lz
  • 111
  • 3
0

A few libraries have this convention. Many programmers come from a background that uses PascalCase for method names (Pascal Case is what you are actually referring to, it is the special case of leading capital camelCase that you mention).

It's incorrect by the python style guide, so I recommend against writing code this way.

marr75
  • 5,666
  • 1
  • 27
  • 41