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.