Differences between _, _ and _ xx _ in python

Source: Internet
Author: User

Differences between _, _ and _ xx _ in python
"_" Single-underline Python does not have a real private method. To implement a private method similar to a method in c ++, you can add an underscore (_) before the method or attribute of the class, which means that the method or attribute should not be called and does not belong to the API. This problem often occurs when using property: class BaseForm (StrAndUnicode ):... def _ get_errors (self): "Returns an ErrorDict for the data provided for the form" if self. _ errors is None: self. full_clean () return self. _ errors = property (_ get_errors) the code snippet above comes from the django source code (django/forms. py ). Here, errors is an attribute that is part of the API, but _ get_errors is private and should not be accessed, but the error result can be accessed through errors. "_" Double underscores (_) can cause more confusion, but they are not used to identify a method or attribute as private. They are used to prevent subclass from overwriting the content. Let's take an example: class A (object): def _ method (self): print "I'm a method in A" def method (self): self. _ method () a = A (). the method () output is as follows: $ python example. py I'm a method in A is good and the expected result is displayed. We add A subclass to A and re-implement A _ method: class B (A): def _ method (self ): print "I'm a method in B" B = B () B. method () Now, the result is as follows: $ python example. pyI'm a method in A, as we can see, B. method () cannot call B. _ method. In fact, it is the normal display of the "_" underline function. Therefore, when we create a method starting with "_", this means that this method cannot be overwritten and can only be used inside the class. In Python? It's easy. It just rename the method, as shown below: a = A () a. _ A _ method () # never use this !! Please! $ Python example. py I'm a method in A if you try to call. _ method: it still cannot run. As mentioned above, you can only call _ method within the class. "_ Xx _" and double underscores (_ xx _). When you see "_ this _", you must never call it. Why? Because it means it is used for Python calls, as follows: >>> name = "igor" >>> name. _ len _ () 4 >>> len (name) 4 >>> number = 10 >>> number. _ add _ (20) 30 >>> number + 20 30 "_ xx _" is often a magic methods operator or local function call. In the preceding example, the function of rewriting class operators is provided. In special cases, it is only a hook called by python. For example, the __init _ () function is called when an object is created and initialized; __new _ () is used to create an instance. Class CrazyNumber (object): def _ init _ (self, n): self. n = n def _ add _ (self, other): return self. n-other def _ sub _ (self, other): return self. n + other def _ str _ (self): return str (self. n) num = CrazyNumber (10) print num #10 print num + 5 #5 print num-20 #30 another example class Room (object ): def _ init _ (self): self. people = [] def add (self, person): self. people. append (person) def _ len _ (self ): Return len (self. people) room = Room () room. add ("Igor") print len (room) #1 Conclusion Using _ one_underline indicates that the method or attribute is private and does not belong to the API; when creating a python call or in some special cases, use _ two_underline __; use _ just_to_underlines to avoid rewriting of the subclass!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.