375

So which is better and why?

def my_function():

or

def myFunction():
9
  • 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.
    – user166390
    Jan 18, 2012 at 10:46
  • 197
    Why the votes to close? This is a perfectly reasonable question about what is considered normal practice in the community.
    – Marcin
    Jan 18, 2012 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. Jan 18, 2012 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, 2013 at 4: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. Feb 12, 2013 at 4:12

3 Answers 3

398

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.

10
  • 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.
    – user559633
    Jul 24, 2013 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. Mar 19, 2015 at 2:15
  • 142
    Of course, Python uses snake_case. That should be obvious.
    – ayke
    Nov 23, 2017 at 20:53
  • 72
    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, 2018 at 7: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 3, 2018 at 9:51
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.

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

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.