A Brief discussion on the naming of variables or functions with _ in Python

  • 2020-06-15 09:22:47
  • OfStack

The Python code style is described by PEP 8. This document describes all aspects of the Python programming style. In compliance with this document, Python code written by different programmers can remain as similar as possible. This makes it easy to read and communicate between programmers.

Identifiers in python can contain Numbers, letters, and _, but must begin with a letter or _, where a name beginning with _ 1 usually has special meaning.

Prefix __ with double underscore __

. Commonly used with special method names to implement 1 of the actions or functions of the object, e.g., the method with ___ 12en__ (), the method with ___ to create an instance, and with ___ 13en__ () to initialize the object,

The x + y operation is mapped to the method x. ___ 19en__ (y), and the index operation x[k] of a sequence or dictionary is mapped to x. ___ 24en__ (k), ___ 26en__ (), and ES27en__ () each with the built-in functions len(), str(), and so on.

Names that begin with the double underscore __ only

A property or method named after it is a private property or private method of a class used for data encapsulation of an object.


class Foo(object):
  def __init__(self):
    self.__name = 'private name'
 
  def getname(self):
    return self.__name
 
  def __spam(self):
    print 'private method'
 
  def bar(self):
    self.__spam()

If a private property or method is accessed externally directly:


>>> f = Foo()
>>> f.__name
 
Traceback (most recent call last):
 File "<pyshell#1>", line 1, in <module>
  f.__name
AttributeError: 'Foo' object has no attribute '__name'
>>> f.__spam()
 
Traceback (most recent call last):
 File "<pyshell#2>", line 1, in <module>
  f.__spam()
AttributeError: 'Foo' object has no attribute '__spam'

This hides the data, but the implementation mechanism is not very strict. It is automated "morphing", with each successive class name starting with a double underscore changing to the new name of "_ class name ":


>>> f._Foo__name
'private name'
>>> f._Foo__spam()
private method

So you can access it.

This mechanism prevents an inherited class from redefining or changing the implementation of a method, for example, by defining a derived class of Foo:


class Goo(Foo):
  def __spam(self):
    print 'private method of Goo'

Rewritten the method with succspam, run:


>>> g = Goo()
>>> g.bar()
private method

The call to the bar() method still executes the Foo class's succspam () method, because in the implementation of the bar() method, self. succspam () has been automatically morphed to ES61en. _Foosuccspam (), and so has the bar() method inherited by Goo.

Names starting with a single underscore _

Commonly used for naming "private" definitions in modules.

The from module import * statement is used to load all the names in the module. To control the imported names, 1 method is to define the list of succall__, and the names in each ___ can only be imported by *,

The other option is to start the definition with a single underscore, which is not imported by *.

Of course, it's also possible to name properties or methods in a class with a single underscore, just to show that the class definer wants the properties or methods to be "private," but it doesn't really matter.

conclusion

Above is the paper on Python with _ variable or function naming all content, hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: