What is the naming convention in Python for variable and function names?

后端 未结 13 2257
生来不讨喜
生来不讨喜 2020-11-22 08:50

Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase:



        
相关标签:
13条回答
  • 2020-11-22 09:19

    I personally use Java's naming conventions when developing in other programming languages as it is consistent and easy to follow. That way I am not continuously struggling over what conventions to use which shouldn't be the hardest part of my project!

    0 讨论(0)
  • 2020-11-22 09:20

    Most python people prefer underscores, but even I am using python since more than 5 years right now, I still do not like them. They just look ugly to me, but maybe that's all the Java in my head.

    I simply like CamelCase better since it fits better with the way classes are named, It feels more logical to have SomeClass.doSomething() than SomeClass.do_something(). If you look around in the global module index in python, you will find both, which is due to the fact that it's a collection of libraries from various sources that grew overtime and not something that was developed by one company like Sun with strict coding rules. I would say the bottom line is: Use whatever you like better, it's just a question of personal taste.

    0 讨论(0)
  • 2020-11-22 09:23

    As mentioned, PEP 8 says to use lower_case_with_underscores for variables, methods and functions.

    I prefer using lower_case_with_underscores for variables and mixedCase for methods and functions makes the code more explicit and readable. Thus following the Zen of Python's "explicit is better than implicit" and "Readability counts"

    0 讨论(0)
  • 2020-11-22 09:25

    David Goodger (in "Code Like a Pythonista" here) describes the PEP 8 recommendations as follows:

    • joined_lower for functions, methods, attributes, variables

    • joined_lower or ALL_CAPS for constants

    • StudlyCaps for classes

    • camelCase only to conform to pre-existing conventions

    0 讨论(0)
  • 2020-11-22 09:25

    As the Style Guide for Python Code admits,

    The naming conventions of Python's library are a bit of a mess, so we'll never get this completely consistent

    Note that this refers just to Python's standard library. If they can't get that consistent, then there hardly is much hope of having a generally-adhered-to convention for all Python code, is there?

    From that, and the discussion here, I would deduce that it's not a horrible sin if one keeps using e.g. Java's or C#'s (clear and well-established) naming conventions for variables and functions when crossing over to Python. Keeping in mind, of course, that it is best to abide with whatever the prevailing style for a codebase / project / team happens to be. As the Python Style Guide points out, internal consistency matters most.

    Feel free to dismiss me as a heretic. :-) Like the OP, I'm not a "Pythonista", not yet anyway.

    0 讨论(0)
  • 2020-11-22 09:27

    further to what @JohnTESlade has answered. Google's python style guide has some pretty neat recommendations,

    Names to Avoid

    • single character names except for counters or iterators
    • dashes (-) in any package/module name
    • \__double_leading_and_trailing_underscore__ names (reserved by Python)

    Naming Convention

    • "Internal" means internal to a module or protected or private within a class.
    • Prepending a single underscore (_) has some support for protecting module variables and functions (not included with import * from). Prepending a double underscore (__) to an instance variable or method effectively serves to make the variable or method private to its class (using name mangling).
    • Place related classes and top-level functions together in a module. Unlike Java, there is no need to limit yourself to one class per module.
    • Use CapWords for class names, but lower_with_under.py for module names. Although there are many existing modules named CapWords.py, this is now discouraged because it's confusing when the module happens to be named after a class. ("wait -- did I write import StringIO or from StringIO import StringIO?")

    Guidelines derived from Guido's Recommendations

    0 讨论(0)
提交回复
热议问题