Showing posts with label single underscore and double underscore meaning in python.. Show all posts
Showing posts with label single underscore and double underscore meaning in python.. Show all posts

Tuesday, 14 November 2017

underscores _ in Python

Understanding the underscore( _ ) of Python :

The underscore ( _ ) is special in Python.

There are 5 cases for using the underscore in Python.

  • For storing the value of last expression in interpreter.
  • For ignoring the specific values. (so-called “I don’t care”)
  • To give special meanings and functions to name of variables or functions.
  • To use as ‘Internationalization(i18n)’ or ‘Localization(l10n)’ functions.
  • To separate the digits of number literal value.


When used in interpreter :

  • The python interpreter stores the last expression value to the special variable called ‘_’.


Example:

>>> x=10

>>> x

10

>>> _

10

>>> _*2

20

>>> _

20

>>> _+3

23

>>> _

23

>>> y=_/3

>>> y

7.666666666666667


For Ignoring the values:


  • The underscore is also used for ignoring the specific values. 
  • If you don’t need the specific values or the values are not used, just assign the values to underscore.


Example:

Ignore a value when unpacking:

>>> x, _, y = (1, 2, 3)

>>> x

1

>>> y

3

>>> 

Ignore the multiple values:


  • Note:which is available in only Python 3.x.


>>> x, *_, y = (1, 2, 3, 4, 5)

>>> x

1

>>> y

5

>>> 

Ignore the index:

for _ in range(10):

print("hai")

hai

hai

hai

hai

hai

hai

hai

hai

hai

hai

def do_something( ):

print("hello")

for _ in range(10):

do_something()

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

Ignore a value of specific location:

>>> x=(5,10,15,20,25)

>>> for _,val in x:

do_something()



Give special meanings to name of variables and functions:

  • The underscore may be most used in ‘naming’.
  • The PEP8 which is Python convention guideline introduces the following 4 naming cases.


single underscore( _ ):

  • leading underscore( _ )

example:

_variablename or _functionname or _methodname or _classname


  • This convention is used for declaring protected variables, functions, methods and classes in a module.



  • tailing underscore( _ )

example:

variablename_ or functionname_ or methodname_ or classname_


  • This convention could be used for avoiding conflict with Python keywords or built-ins.


Tkinter.Toplevel(master, class_='ClassName')
# Avoid conflict with 'class' keyword

list_ = List.objects.get(1)
# Avoid conflict with 'list' built-in type


double underscore( _ _ ):


  • leading double underscore(_ _)

example:
 _ _varaiblename or _ _functionname or _ _methodname or _ _classname


  • double underscore will mangle the attribute names of a class to avoide the conflicts of attribute names between classes.
  • (so-called “mangling” that means that the compiler or interpreter modify the variables or function names with some rules, not use as it is). 
  • The mangling rule of Python is adding the “_ClassName” to front of attribute names are declared with double underscore. 
  • That is, if you write method named “__method” in a class, the name will be mangled in “_ClassName__method” form.



  • leading and tailing double underscores(_ _)

example:
_ _variablename_ _ or _ _functionname_ _ or _ _classname_ _ or __methodname__



  • This convention is used for special variables or methods (so-called “magic method”) such as   _ _init_ _  ,  _ _len__  ,          _ _new_ _ ,_ _del_ _,......etc.
  • These methods provides special syntactic features or does special things.


example:
class A:
    def __init__(self, a):
          self.a = a
    def __custom__(self):
               pass


As Internationalization(i18n)/Localization(l10 n) functions :


  • The built-in library gettext which is for i18n/l10n uses this convention, and Django which is Python web framework supports i18n/l10n also introduces and uses this convention.


# see official docs : https://docs.python.org/3/library/gettext.html import gettext
gettext.bindtextdomain('myapplication','/path/to/my/language /directory') gettext.textdomain('myapplication') _ = gettext.gettext
# ...
print(_('This is a translatable string.'))



To separate the digits of number literal value:

  • This feature was added in Python 3.6. It is used for separating digits of numbers using underscore for readability.


>>> x=1_00

>>> x

100

>>> y="siva_krishna"

>>> y

'siva_krishna'

>>> z=0b_101

>>> bin(z)

'0b101'
>>>