Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use single underscore in python

Shulou Source: shulou.com Published: 2022-06-01 04:23:46 09月18日 Update

This article will explain in detail how to use single underlining in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Single underscore (_)

1. In the interpreter: in this case, "_" represents the result of the previous statement executed in the interactive interpreter session. This usage is first adopted by the standard CPython interpreter, and then by other types of interpreters.

> _ Traceback (most recent call last): File ", line 1, in NameError: name'_'is not defined > 42 > _ 42 > 'alrightlighting' If _ else': (''alrightships' > _ 'alrightships'

2. As a name: this is slightly related to the above, and "" is used as a temporary name at this time. In this way, when others read your code, they will know that you have assigned a specific name, but will not use that name again later. For example, in the following example, you may not be interested in the actual value in the loop count, so you can use "".

N = 42for _ in range (n): do_something ()

3. Internationalization: you may have seen "_" used as a function. In this case, it is usually used to implement the function name of translation lookup between internationalized and localized strings, which seems to derive from and follow the corresponding C convention. For example, in the "conversion" section of the Django document, you will see the following code:

From django.utils.translation import ugettext as _ from django.http import HttpResponse def my_view (request): output = _ ("Welcome to my site.") Return HttpResponse (output)

You can find that the usage in scenario 2 and scenario 3 may conflict with each other, so we need to avoid using "" as the temporary name in code blocks that use "" as the internationalized lookup transformation function.

This is the end of the article on "how to use single underline in python". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Names interpreters interpreters underscores codes international articles functions scenarios situations more good practical temporary between interactive representative usage examples successively Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple NVidia Redmi Shulou Tech Info