Sunday, December 19, 2010

Understanding Python naming scheme - Wisdom + perseverance = omnipotent

If you have already guessed, you can not see me this blog. If you have not guessed, or heart, of doubt, this blog that I had prepared it for you.
why all the Output \When the variable name is bound to an object, the variable name to refer to this object, as human societies, 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 , it is a global variable. Module I believe we have a good understanding, but the block may be more confusing. Here explain:
block unit is available as a Python executable program text; module body of the function and class definitions are blocks of code. Not only that, each is an interactive script command block; a script file is a block of code; a command line script is a code block.
next turn to the visible variables , we introduce a range of concepts. the scope is the variable name in the code block visibility. If a block of code defined in the local variable, and that the scope to include this block of code. If the variable is defined in a function code block, that the scope of extended to the function block in any block of code, unless one of the other variables are defined with the same name. But the variables defined in the scope of the class is limited to blocks of code in the class, but does not extend to the method code block.
Behind
According to the theory of the previous section, we can put the code into three blocks: the definition of class A, class B definition and the definition of the variable b. according to class definition, we know that the code defines a class A three member variables (Python functions are objects, so the members is known as member variables also work.); Class B defines two member variables. This can be verified by the following code:>>> print '\ n '. join (dir (A)) _A__private__init__public>>> print' \ n '. join (dir (B)) _A__private_B__private__init__public
Hey, why there is a class A _A__private the Attribute named it? and _ _private disappeared! This would be to talk about Python rolled a private variable.
explore
understand Python Python's friends all know the order of two or more underscore characters and not to under two or more marking the end of the variable as private variables. private variables in the code generated before being converted to long form (become public.) conversion mechanism is this: in the variable front-end into the class name, then add an underscore character in front of . This is called the private variables rolled (Private name mangling). such as class A in the __private identifier will be converted to _A__private, this is the last one there _A__private and __private reason behind the disappearance.
revisit the two digression:
rolled first, because the identifier will longer, when more than 255 times, Python will be cut off, pay attention to naming conflicts resulting therefrom.
The second is when the class name all of the following program line when named, Python no longer perform rolled. If:
code
Now we come back to see why the output \believe that intelligent readers have guessed the answer, right? If you did not think, I'll give you a hint: the truth with the C macro language similar pretreatment.
because the class A defines a private member function (variable) Therefore, prior to the implementation of the code generation rolled private variables (noted in the previous section that the red lines mark is not?). rolling pressure, the class A, so the code becomes:
code class A (object ): def __init__ (self): self._A__private () # this line changed self.public () def _A__private (self): # this line has changed print 'A.__private ()' def public (self): print 'A.public ()'
is not a bit like a C language macro expansion ah?
defined as the time in class B does not cover the __init__ method, so the call is still A.__init__, that the implementation of the self._A__private (), the natural output \evaluation)
at one hundred twenty-three thousand four hundred fifty-six Statistics

No comments:

Post a Comment