I am new to Python as you might tell. I have read various documents but I still can not figure out if there's a "naming best practices"
for strings functions and of course, classes.
If I want to name a class or a function as a SiteMap, is it ok to use SiteMap? Should it be Site_map or any other thing, for example?
Thank you!
PS. any further reading resource is GREATLY appreciated! PS. I am doing web-app development (learning, better to say!)
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
Naming Convention for Functions So, similar to variables, the camel case approach is the recommended way to declare function names. In addition to that, you should use descriptive nouns and verbs as prefixes. For example, if we declare a function to retrieve a name, the function name should be getName.
Use the __name__ Property to Get the Function Name in Python To access the __name__ property, just put in the function name without the parentheses and use the property accessor . __name__ . It will then return the function name as a string.
Naming Conventions:
There are various Python naming conventions I use. Consistency here is certainly good as it helps to identify what sort of object names point to. I think the conventions I use basically follow PEP8.
1) Module names should be lowercase with underscores instead of spaces. (And should be valid module names for importing.)
2) Variable names and function/method names should also be lowercase with underscores to separate words.
3) Class names should be CamelCase (uppercase letter to start with, words run together, each starting with an uppercase letter).
4) Module constants should be all uppercase.
E.g. You would typically have module.ClassName.method_name
.
5) Module names in CamelCase with a main class name identical to the
module name are annoying. (e.g. ConfigParser.ConfigParser, which
should always be spelt configobj.ConfigObj
.)
6) Also, variables, functions, methods and classes which aren't part of your public API, should begin with a single underscore. (using double underscores to make attributes private almost always turns out to be a mistake - especially for testability.)
Whitespace
And finally, you should always have whitespace around operators and after punctuation. The exception is default arguments to methods and functions.
E.g. def function(default=argument): and x = a * b + c
PEP8 specifies recommended naming convention for Python. Among rules discussed there, it mentions underscore_names
for functions and variables (regardless of their type), and CamelCase
for classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With