Module Name:
Lowercase letters, dividing between words with _
ad_stats.py
Package Name:
Same as the module name
Class Name:
Capitalize the first letter of the word
Adstats
Configutil
global variable names (class variables, equivalent to static variables in Java):
Uppercase letters, dividing between words with _
Number
Color_write
Common variables:
Lowercase letters, dividing between words with _
This_is_a_var
Instance variable:
Start with _, the rest is the same as the normal variable
_price
_instance_var
Common functions:
As with normal variables:
Get_name ()
Count_number ()
Ad_stat ()
Private function (external access will error):
Starts with __ (2 underscores), and the rest is the same as the normal function
__get_name () Here someone suggested an underscore, and someone suggested two underscores. Here is a reply that is more convincing:
"Defining a private variable in Python requires only a" __ "(two underscores) before the variable name or function name, and then it becomes private. Internally, Python uses a name mangling technology to replace __membername with _classname__membername, so you'll be prompted to find it when you use the name of the original private member externally. ”
"Whether it's a single underscore or a double-underlined member, you want the external program developer not to directly use these member variables and these member functions, but double underscores are syntactically more straightforward to avoid incorrect use, but can still be accessed if you follow the _ class name __ member name. A single underline may be handy for dynamic debugging, so it might be better to use a single underline as long as the members of the project group are not directly using the member that starts with the underscore. ”
1. Comparisons to singletons-like-None should always be do with are or is not, never the equality operators.
Also, beware of writing if x when you really mean if x are not None--e.g. when testing whether a variable or argument That defaults to None is set to some other value. The other value might has a type (such as a container) that could is false in a Boolean context!
2. When catching exceptions, mention specific exceptions whenever possible instead of using a bare except:clause.
For example, use:
Try
Import Platform_specific_module
Except Importerror:
Platform_specific_module = None
A bare except:clause would catch systemexit and keyboardinterrupt exceptions, making it harder to interrupt a program with Control-c, and can disguise other problems. If you want to catch all exceptions this signal program errors, use except Exception: (bare except are equivalent to except Baseexception:).
3. For sequences, (strings, lists, tuples), use the fact, empty sequences is false.
Yes:if not seq:
If seq:
No:if Len (seq)
If not Len (seq)
4. Context managers should be invoked through separate functions or methods whenever they doing something other than acquire and release resources. For example:
Yes:
With Conn.begin_transaction ():
Do_stuff_in_transaction (conn)
No:
With Conn:
Do_stuff_in_transaction (conn)
The latter example doesn ' t provide any information to indicate that the __enter__ and __exit__ methods is doing something Other than closing the connection after a transaction. Being Explicit is important in the this case.
5. Don ' t compare Boolean values to True or False using = =.
Yes:if Greeting:
No:if greeting = = True:
Worse:if Greeting is True:
From
http://luochunfeng163.blog.163.com/blog/static/167009249201362453358567/
Http://www.cnblogs.com/ToDoToTry/archive/2012/11/27/python_naming_conventions.html
Http://www.educity.cn/wenda/354157.html
Python coding specification "pick"