In naming the variables Python bags Method

Module name: 
between lowercase letters, words divided by _ 
ad_stats.py 

package name: 
and the same module name 

class name
word capitalized 
AdStats 
configutil 

global variable name (class variables, static variables in java equivalent): 
uppercase, _ split between words with 
NUMBER 
COLOR_WRITE 

ordinary variables: 
lowercase letters, words with _ divided between 
this_is_a_var 

instance variables: 
start with _, and other variables, like ordinary 
_price    
_instance_var 

private instance variables (external access being given): 
beginning with __ ( 2 underscore), and other common variables as 
__private_var 

proprietary variable: 
__ beginning, the end __, usually python own variables, not named in this way 
__doc__ 
__class__ 

common functions
and as ordinary variables: 
get_name () 
count_number () 
ad_stat () 

Private function (external access being given): 
beginning with __ (two underscores), and other normal functions 
__get_name 
() ----------------------- ---------------------------------------------
file name 
all lowercase, can use the underscore 
package 
should be short, lowercase names. If underscore can be added to improve readability. As mypackage. 
Module 
specifications and the same package. As mymodule. 
Class 
always use the first letter capitalized word string. Such as MyClass. You can use an extra inner class leading underscore. 

Functions & Methods 
Function names should be lowercase, you can use the underline style word to increase readability. Such as: myfunction, my_example_function. 
* * Note: mixed case is allowed only for time already take advantage of this style, in order to maintain backward compatibility. 
Function parameters and methods 
always use "self" as an example of the method of the first parameter. Always use "cls" class method as the first parameter.  
If the parameter name of a function and retention of key conflict, usually a good underscore suffix to use abbreviations or strange spelling. 
Global variables 
for from M import * import statements, if you want to stop global variable import module can use the old standard, on a global variable plus a leading underscore. 
* * Note: Avoid using global variables 
variables 
Variable names all lowercase, connected by underlining each word. As = the WHITE Color, this_is_a_variable =. 1 
* * Note: 
1. Whether a class member variable or global variable, m or g prefix not used. 
2. The private class members to use a single underscore prefix to identify, define and more public members, less-defined private members. 
3. The variable name with the type of information should not be, because Python is dynamically typed language. As iValue, names_list, dict_obj, etc. are bad name. 
Constant 
constant name in all capital letters, each connected by underlining words such as MAX_OVERFLOW, TOTAL. 
Abnormal 
to "Error" as a suffix. 
Abbreviations 
situation should try to use the full name word spelling, abbreviations of the following two ways: 
1. Common abbreviations, such as XML, ID, etc., when naming should be only the first letter capitalized, such as XmlParser. 
2. Name contains long words on a word abbreviations. At this convention ChengSu abbreviations should be used. 
For example: 
function abbreviated as the Fn 
text abbreviated as TXT 
Object abbreviated obj 
COUNT abbreviated cnt 
Number The abbreviated num, and so on. 
Leading underscore suffix 
a leading underscore: that non-public. 
A suffix underlined: avoid keyword conflicts. 
Two leading underscores: Used when naming a class attribute name cause conflict. 
Two leading and trailing underscore: "Magic" (special purpose) other objects or properties, e.g. __init__ or __file__. Never create such a name, but only to use them. 
* * Note: There is some controversy about the use of an underscore. 
Python designated special variables with an underscore as a variable prefix and suffix. 

_xxx not use 'from module import *' import 
__xxx__ system-defined name of 
private variable names __xxx class 

core Style: avoid using the underscore as the beginning of a variable name. 

Because underscore has a special meaning to the interpreter, and a built-in symbolic identifiers used, we recommend that programmers avoid using the underscore as the beginning of a variable name. In general, variable names _xxx be seen as "private", can not be used outside the module or class. When the variable is private time with _xxx represent variable is a good habit. Because the variable names __xxx__ for Python for
member variables "single underline" the beginning of protection is called variable, meaning that only the class and subclass object objects they can access these variables; 
"double underline" the beginning of a private members, meaning only you can access the class object, even subclass object does not have access to this data. 

Single underscore (the _foo) representative can not directly access the class attributes, through an interface class provides access, not the introduction "from xxx import *"; double underscore private members of the representative (__foo) class; in (__foo__) on behalf of python is double-underlined in particular the beginning and end of the specific identification method, such as __init __ () representative of the class constructor. 
Specific naming 
mainly refers to the system of reserved words __xxx__ form of nomenclature. The project can also use this name, its significance lies in this form of the variable is read-only, this form of class member functions so as not to overload. Such as 
class Base (Object): 
DEF __init __ (Self, the above mentioned id, parent = None): 
Self .__ id__ = the above mentioned id 
Self .__ parent__ is = parent 
DEF __message __ (Self, msgid): 
# ... slightly 
among __id __, __ parent__ and __message__ have adopted system reserved word nomenclature. 
Attached: Google Python naming 
module_name, package_name, ClassName, method_name, exceptionName, function_name, GLOBAL_VAR_NAME, instance_var_name, function_parameter_name, 
local_var_name. ------------------------ --------------------------------
from: HTTP: //hi.baidu.com/kxw102/blog/item/ 212e9f3859202fe33b87ce4b.html 
understand Python naming 

primer 
I warmly invite everyone to guess the following program output: 
class a (Object): 
       DEF __init __ (Self): 
              Self .__ Private () 
              self.public () 
       DEF __private (Self): 
              Print 'Private .__ A ()' 
       DEF public (Self): 
              Print 'A.public ()' 
class B (A): 
       DEF __private (Self): 
              Print 'B .__ Private () ' 
       DEF public (Self): 
              Print' B.public () ' 
b = B () 

on the 
correct answer is: 
a .__ Private () 
B.public () 
if you've guessed it, you can not look at me this blog up. If you do not have to guess or have a question about my heart, that my this blog is for you prepared. 
Why everything will be output from the "A .__ private ()" begins. But talk clearly why it is necessary that we look at Python naming scheme. 
According Python manual, variable name (identifier) is an atomic element Python. When the variable name is bound to an object, and variable names to refer to this object, as human society, is not it? When the variable name appears in the code block, then it is a local variable; when the variable name appears in the module, which is a global variable. Module I believe we have a good understanding, but the code block may be confusing some. Here to explain: 
Python is a section of code blocks can be used as an executable program text unit; module function class definition body and the all code blocks. Not only that, each interaction script command is a code block; a script file is a block of code; a command-line script is a block of code. 
Then talk about the visibility of variables, we introduce the concept of a range. It is in the range of visibility of the variable name code block. If a code block defined in a local variable, then the block range including. If the variable is defined in one functional block, the range that would be extended to the function block is a block to any other except where defined variable with the same name. The scope is defined in the class variable is defined in the class code block, but does not extend to the method code block. 

Behind 
According to the theory of the previous section, we code can be divided into three code blocks: definition of class A, class definitions, and definitions of the variable b B. The class definition, we know that the code to the class A member defines three variables (Python is a function of the object, the method is called a member of member variables also work.); Class B defines two member variables. This can be verified by the following code: 
>>> Print '\ n'.join (the dir (A)) 
_A__private 
the __init__ 
public 
>>> Print' \ n'.join (the dir (B)) 
_A__private 
_B__private 
the __init__ 
public 
Hey, why the class there Attribute a named _A__private it? And __private disappeared! It is necessary to talk about Python's private variables rolling pressed. 

Probe 
Understand Python's friends all know Python to begin with two or more underscore characters and no two or more variables to underscore ending as private variables. Private variable length format are converted to (becomes public) prior to code generation. Conversion mechanism is this: the front end of the insertion in the variable class name, then add an underscore character at the front end. This is called private variables rolling pressure (Private name mangling). As in the class A __private identifiers will be converted to _A__private, this is the one reason _A__private and __private disappearance appeared. 
Revisit two points the way: 
first, because the pressure will roll identifier longer, when more than 255 time, Python will be cut off, pay attention so named due to the conflict. 
Second, when the class name with an underscore naming all the time, Python will no longer perform rolling press. Such as: 
>>> class ____ (Object): 
       DEF the __init __ (Self): 
              Self .__ Method () 
       DEF __method (Self): 
              Print '____.__ Method ()' 
>>> Print '\ n'.join (the dir (____ )) 
__class__ 
__delattr__ 
__dict__ 
__doc__ 
__getattribute__ 
__hash__ 
__init__ 
__method # is not rolling a 
__module__ 
__new__ 
__reduce__ 
__reduce_ex__ 
__repr__ 
__setattr__ 
__str__ 
__weakref__ 
>>> obj = ____ () 
____.__ method () 
>>> obj .__ Method, () # can call external 
____.__ method () 
Now why would we go back to the output "A. __private () "now! 

The truth 
believe it smart readers have already guessed the answer, right? If you have not thought of, I'll give you a hint: the truth with C language macro pretreatment almost. 
A class is defined as a private member function (variable), so the first execution before rolling a private variable code generation (noticed a marked red lines that do not?). After rolling a code class A becomes the case: 
class A (Object): 
       DEF the __init __ (Self): 
              self._A__private () # line changed 
              self.public () 
       DEF _A__private (Self): # line It has changed 
              Print 'Private .__ a ()' 
       DEF public (Self): 
              print 'A.public ()' 
is not it a bit like a C language macro expansion ah? 
When B is defined as the class not covered __init__ method, the call is still A .__ init__, i.e. executed self._A__private (), a natural output "A .__ private ()" up. 
The following two pieces of code can increase persuasiveness, enhance mutual understanding: 
>>> class C (A): 
       DEF __init __ (Self): # rewrite __init__, is no longer called self._A__private 
              Self .__ Private () # bind here is _C_private 
              self.public () 
       DEF __private (Self): 
              Print 'C .__ Private ()' 
       DEF public (Self): 
              Print 'C.public ()' 
>>> C = C () 
C .__ Private () 
C .public () 
############################ 
>>> A class (Object): 
       DEF the __init __ (Self): 
              self._A__private () # call the function, Python will not have a defined it to me ~ ^ _ ^ 
              self.public () 
       DEF __private (Self): 
              Print 'A .__ Private ()' 
       DEF public (Self): 
              Print 'A.public ()' 
>>> A = A () 
A .__ Private () 
A.public ()

Guess you like

Origin www.cnblogs.com/shujuxiong/p/11229023.html