Underline/privatization __python in Python

Source: Internet
Author: User
1 Single underline before name (_variablename)

An instance variable name that starts with an underscore, for example _name, such an instance variable outside is accessible, but, according to the conventions, when you see such a variable, meaning, "although I can be accessed, but, please treat me as a private variable, do not randomly access."

Class Myfilter (object):

    def init (self):
        self._blocked = []

    def filter (self, sequence): Return
        [x for X In sequence if x self._blocked]


class Redfilter (myfilter):
    def init (self):
        self._blocked = [' Red ']
  f = Redfilter ()
f.init ()
sequence = [' Red ', ' Blue ', ' white ', ' black ', ' Pink ']
print ' The sequence is%s\n ' % sequence
print ' We want to filter%s\n '% f._blocked
print ':%s '% f.filter (sequence)

2 double underline before name (__variablename)

The use of the first double underline (__) of a name (specifically a method name) is not a convention, and it has a specific meaning for the interpreter. This usage in Python is intended to avoid a name conflict with a subclass definition. The Python document states that any identifier of the form "__spam" (at least two leading underscores, up to a subsequent underscore) will be replaced by the "_classname__spam" form, where "classname" is the current class name that removes the leading underscore.

Class Myfilter (object):

    def init (self):
        self.__blocked = []

    def filter (self, sequence): Return
        [x for X In sequence if x self.__blocked]


class Redfilter (myfilter):
    def init (self):
        self.__blocked = [' Red ']< C7/>f = Redfilter ()
f.init ()
sequence = [' Red ', ' Blue ', ' white ', ' black ', ' Pink ']
print ' The sequence is%s\n '% sequence
print ' We want to filter%s\n '% f.__blocked
print ':%s '% f.filter (sequence)

3 double underline before and after the name (init)

The end of a double underline, is a special variable, the special variable can be directly accessed, not private variables, so you can not use name,score such a variable name. For example, when you define a class, you often override the "init" method, which is a special method.

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.