372

So which is better and why?

def my_function():

or

def myFunction():
bad_coder
  • 11,289
  • 20
  • 44
  • 72
tdc
  • 8,219
  • 11
  • 41
  • 63
  • 6
    FWIW, I think this is covered as "doesn't matter" in one of the PEPs. Either seems to be acceptable from my experience (just *be consistent*). In any case, this is fairly subjective. –  Jan 18 '12 at 10:46
  • 196
    Why the votes to close? This is a perfectly reasonable question about what is considered normal practice in the community. – Marcin Jan 18 '12 at 10:50
  • 4
    there are as many valid coding convention as developers, the only important thing is to **keep the same coding convention** in a project. – Cédric Julien Jan 18 '12 at 10:50
  • 40
    I disagree with those who said it "doesn't matter". the PEP8 convention is widely adhered to. Also, I'm *amazed* this is closed as not constructive, if you do a "camelcase in python" google search this is the first thing that pops up! – Mike Vella Jan 30 '13 at 04:08
  • 15
    I think this question causes tension because The Specification says to use underscores, but most professional coders use camel case every day (in various languages other than python). Underscores seem to be approaching the end of their lifecycle, alongside so many C++ repositories that adhere to the convention. – Neal Ehardt Feb 12 '13 at 04:12
  • 2
    Ruby (and apparently PHP) mostly adhere to underscore as well, so I don't think underscores are really leaving too soon... I don't think the author is proposing a flamewar (though 'which is better and why' could be reworded to make it clear), he's simply trying to understand what is the (community-)preferred way in a new language. – malvim Aug 01 '14 at 20:03
  • 2
    @MikeVella. So if you swap from one to another what will realistically happen? Nothing. It may make a marginal difference in readability, but not much. The only thing is that the function might be confused with a class name if it is in camel case. But then you have the same thing with variables and functions. We pay far too much attention to trivial nonsense like this. – wobbily_col Mar 18 '15 at 10:24
  • I'm starting in Python and came here for the same reason. I used to advocate CamelCase for classes and camelCase for functions... – Arkt8 May 24 '21 at 01:07

3 Answers3

395

for everything related to Python's style guide: i'd recommend you read PEP8.

To answer your question:

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

JMax
  • 26,109
  • 12
  • 69
  • 88
aayoubi
  • 11,285
  • 3
  • 22
  • 20
  • 105
    You clipped an important part of PEP8: "mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility." Sometimes, CamelCase is acceptable. –  Jul 24 '13 at 18:04
  • 25
    Which is absolutely contradictory with the fact that a camel cased group of words form an unambiguous solid symbol that looks like a single object matching the idea that a method name is one thing (as opposed to several objects, words, that have to be read and later interpreted as one single group of several things). That's even worst for _ if the method name contains a reserved word in it. – Sebastian Sastre Mar 19 '15 at 02:15
  • 2
    @SebastianSastre visual word grouping is typically solved using syntax highlighting, though. – Joost Apr 27 '15 at 13:22
  • 138
    Of course, *Python* uses snake_case. That should be obvious. – ayke Nov 23 '17 at 20:53
  • 3
    I use camelCase for function names and snake_case for variables. Am I the devil? – Matt Fletcher Jan 04 '18 at 11:10
  • 71
    I don't get why underscores improve readability while Microsoft's Framework Guide claims Camel Case improves readability. Are python developers and C# developers two kinds of species? – Gqqnbig Mar 24 '18 at 07:07
  • 7
    @Gqqnbig I think the intended meaning of the quoted part is that lowercase-with-underscores is more readable than lowercase-without-underscores. I think if it meant that snake_case is more readable than CamelCase the comma would be after "underscores" not after "lowercase". – Rich Dec 03 '18 at 09:51
  • While I go with the underscore of function names as mentioned in PEP8, I am finding that in 2020 there are more and more tutorials/snippets out on the web that are showing camel case. My guess is a lot of people are coming to Python from JavaScript. Who knows. Coming from Java back around 2000 or so, I had gotten corrected for using camel case and so I adjusted for readability sake. My opinion however is it's more important to not use wordy or very long function names, came case or not. The only devil imo is the word, "should." – Harlin Feb 09 '20 at 13:00
  • 1
    On "prevailing style" and camel case (see response from user559633 at the top), I remember at work a particular library was written as a wrapper around the original implementation of a Java package. To keep consistency with the Java code, the team who wrote the wrapper kept the code in camel case so it was easy to find corresponding functions/methods in Python. – Harlin Feb 09 '20 at 13:04
  • Ok, according to the (in)famous `PEP 8`: function names should be in lower case with '_'. But what about method names? – TrueY Feb 20 '22 at 12:37
49

PEP 8 advises the first form for readability. You can find it here.

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Florent Roques
  • 2,424
  • 2
  • 20
  • 23
David M
  • 71,481
  • 13
  • 158
  • 186
29

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

Check out its already been answered, click here

Community
  • 1
  • 1
Harish Kurup
  • 7,257
  • 19
  • 65
  • 94