The meaning of single underline and double underline in Python class

Source: Internet
Author: User

# "Single underline"   The starting member variable is called a protection variable, meaning that only class objects and subclass objects can access these variables themselves; # "Double underline"   begins with a private member, meaning that only the class object can access it, and even the subclass object cannot access the data. Class pub (object):    var1 =  ' Hello '     _var2 =   ' yes '     __var3 =  ' hah '     def __init__ (self):         self._a = 123         self.__b = 456        self.c = 789     def show_a_var (self):        return  Self._a    def show_b_var (self):         Return self.__b    def show_var2 (self):         return self._var2    def show_var3 (self):         return self.__Var3class foo (Pub):     passprint (PUB.VAR1) sl = pub () print (SL.C) print ( Sl.show_a_var ()) print (Sl.show_b_var ()) print (SL.SHOW_VAR2 ()) print (SL.SHOW_VAR3 ()) Fa = foo () print (fa._ VAR2) print (fa.var1) print (Fa.show_a_var ()) print (FA.SHOW_VAR2 ())


This article is from the Fresh Air Team blog, so be sure to keep this source http://freshair.blog.51cto.com/8272891/1930129

The meaning of single underline and double underline in Python class

Related Article

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.