Introduction to the use of underscores in Python

Source: Internet
Author: User

Single Dash (_)

In the Interpreter

In the Interpreter, _ represents the result of the execution of the previous article in an interactive interpreter session. This usage is somewhat similar to the use of the previous command in Linux. Just the result of the last article represented in the Python interpreter.


>>> "Alright"
' Alright '
>>> _
' Alright

As a name

Used as a temporary name, but it will not be used again later. This usage is often used in loops.


For _ in range (10):
Do_something ()

As a function of the connector, is only a function name naming method, just as Java's hump-style nomenclature is the same.


def add_user (user):
Do_something

Single dash before name (_get_content)

The explanation in the Python documentation is that the following names (such as _get_content) that are prefixed by an underscore should be part of the API that is not exposed, whether it is a function, a method, or an attribute. They should be seen as an implementation detail at this time, and they need not be externally notified when they are modified.

Names that begin with _ are not imported when you use the From < module/package name > Import *, unless the __all__ list in the module or package explicitly contains them when you use the name with the following underscore _ as the prefix.
Since this type of writing merely represents a detail implementation, it can still be inherited when the class inherits.

class people (object):
def _eat (self):
Print (' I am eating ')

Class Student (People):
@property
def birth (self):
Return Self._brith

@birth. Setter
def birth (Self,value):
Self._birth = value

@property
def age (self):
Return Self._age
s = Student ()
S._eat () #输出: I am Eating

Double underline before name (for example: __run)

A variable preceded by a double underline, representing a private function that cannot be inherited or accessed externally.


class people (object):
def _eat (self):
Print (' I am eating ')
def __run (self):
Print (' I can run ')

Class Student (People):
def torun (self):
Self.__run () #出错, because the people method cannot be inherited, the __run () method does not exist in student

s = Student ()
S.torun ()
p = people ()
P.__run () #出错, because private functions cannot be accessed externally

The Python learning Guide explains that variable names that begin with a double underscore automatically expand to include the name of the class in which it is located. For example, a variable name such as x in the spam class is automatically programmed _SPAMX: The original variable name adds an underscore to the header and then the class name.


Dir (People) #输出: _people__run,_eat,...

Double underline before and after name (for example, init)

A double underline before and after is a special function. You can often copy these methods to achieve the functionality that you need. The most common is the replication __init__ method.


Class People (object):
    def __init__ (self, arg):
         super (people, self). __init__ ()
        self.arg = arg

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.