Search code examples
pythonpep8

Naming attributes and methods


From PEP 8

Method Names and Instance Variables

Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.

By this PEP guide the following code should be OK.

class MyClass():
    def __init__(self):
        self.set_alarm = object
        self.cancel_alarm = object

    def set_alarm(self):
        pass

    def cancel_alarm(self):
        pass

But of course it won't work, what's the advise in such cases? use mixedCase for the function name?

I don't understand why underscore_snake is preferred over mixedCase. In the same pep it states that

mixedCase is allowed only in contexts where that's already the prevailing style


Solution

  • It's preferred because it's preferred. Casing rules are almost always arbitrary and vary from community to community. The main goal is consistency between various libraries. For instance, without this rule you could have something like this:

    s.Foo = bar_2.oogaBooga()
    s.Bar = BAZ.BAZOID()
    q = spam.egg_soup()
    

    Where oogaBooga, BAZOID, and egg_soup each come from different libraries. This would make code harder to read.