简体   繁体   中英

do you see CamelCase much in Python methods and functions?

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?

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 (eg 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.

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).

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.

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM