python- magical underscore

2019-12-16 22:45:29

There are various python underlined the role of this chapter to introduce the various functions are underlined.

 

First, at the beginning of the single underscore _VAR

The beginning of the single underscore or very common, expressed as a variable protection / protective function, but note that this is just a convention, this is not a mandatory requirement of Python in the class.

In other words, you can still access a class variable or method to begin with a single underscore without error.

class a:
    def __init__(self):
        self.a = 1
        self._b = 2

    def _func(self):
        print("Hello world.")
        

if __name__ == "__main__":
    aa = a()
    print(aa.a)
    print(aa._b)
    aa._func()

The above code is run completely without any problems,This is because a single underscore prefix Python is merely a convention - at least with respect in terms of variables and method names.

However, it will certainly affect the way leading underscore imported names from the module.

Suppose you have the following code in a module called my_module in:

# This is my_module.py:

def external_func():
   return 23

def _internal_func():
   return 42

Now, if you use a wildcard import all names from the module does not import the Python name with a leading underscore:

>>> from my_module import *
>>> external_func()
23
>>> _internal_func()
NameError: "name '_internal_func' is not defined"

Of course, if you are not using the Import wildcard, then still will not be any problems, in fact, we do not recommend using wildcards way to import methods.

 

Second, begin with a double underscore __VAR

At the beginning of the above single underline somewhat similar to the gentlemen's agreement, to see is to tell you this is private and can not be accessed outside of, but in fact you want to access external Mozhe. However, the beginning of the double-underlined not the same, the fixed pattern of use in python.

Double underscore prefix will cause the Python interpreter to rewrite attribute name to avoid naming conflicts subclass.

python will be rewritten (_ClassName__VAR) to name a variable or method beginning with a double underscore, when the external access directly in accordance with the variable name if access will be given.

class Test:
    def __init__(self):
       self.foo = 11
       self._bar = 23
       self.__baz = 23

    def get_var(self):
        print (self .__ baz) # can directly access the internal
    
    def __method(self):
        print("This is method.")


if __name__ == "__main__":
    a = Test()

    # print(a.__baz) # AttributeError: 'Test' object has no attribute '__baz'
    print(a._Test__baz)

    # Internal transparent
    a.get_var()

    # a.__method() # AttributeError: 'Test' object has no attribute '__method'
    a._Test__method()

  

Third, at the beginning of the end of the double underscore

If a name while double underscore start and end, it will not modify the application name. The double underlined variables prefix and suffix are not surrounded Python interpreter Review:

class PrefixPostfixTest:
   def __init__(self):
       self.__bam__ = 42

>>> PrefixPostfixTest().__bam__
42

However, Python retained the double leading and double underlined at the end of the name, used for special purposes. Examples include, __ init__ object constructor, or an object such that the __call__ --- It can be invoked.

These methods are commonly known as magic dunder method - but the Python community many people (myself included) do not like this approach.

Best to avoid a double underscore ( "dunders") and names that end in the beginning of their program in order to avoid conflicts with future changes in the Python language.

 

Guess you like

Origin www.cnblogs.com/hyserendipity/p/12052107.html