What Are Those Underscores?

Any method (or other name) beginning and ending with two underscores is considered special by Python. The help documentation for strings shows these methods, among many others:

 | Methods defined here:
 |
 | __add__(self, value, /)
 | Return self+value.

These methods are typically connected with some other syntax in Python: use of that syntax will trigger a method call. For example, string method __add__ is called when anything is added to a string:

 >>>​​ ​​'TTA'​​ ​​+​​ ​​'GGG'
 'TTAGGG'
 >>>​​ ​​'TTA'​​.__add__(​​'GGG'​​)
 'TTAGGG'

Programmers almost never call these special methods directly, but it is eye-opening to see this and it may help you to understand how Python works.

Integers and floating-point numbers have similar features. Here is part of the help documentation for int:

 Help on class int in module builtins:
 
 class int(object)
 ...
  | Methods defined here:
  |
  | __abs__(self, /)
  | abs(self)
  |
  | __add__(self, value, /)
  | Return self+value.
  |
  | __gt__(self, value, /)
  | Return self>value.

The documentation describes when these are called. Here we show both versions of getting the absolute value of a number:

 >>>​​ ​​abs(-3)
 3
 >>>​​ ​​(-3).__abs__()
 3

We put -3 in parentheses so that Python will call __abs__ after negating 3. Without the parentheses __abs__ is called first and the result is negated, which leads to an unexpected result:

 >>>​​ ​​-3​​ ​​.__abs__()
 -3

This is functionally equivalent to:

 >>>​​ ​​-(3​​ ​​.__abs__())
 -3

We need to put a space after 3 so that Python doesn’t think we’re making a floating-point number 3. (remember that we can leave off the trailing 0).

Let’s add two integers using this trick:

 >>>​​ ​​3​​ ​​+​​ ​​5
 8
 >>>​​ ​​3​​ ​​.__add__(5)
 8

And here we compare two numbers to see whether one is bigger than the other:

 >>>​​ ​​3​​ ​​>​​ ​​5
 False
 >>>​​ ​​3​​ ​​.__gt__(5)
 False
 >>>​​ ​​5​​ ​​>​​ ​​3
 True
 >>>​​ ​​5​​ ​​.__gt__(3)
 True

Again, programmers don’t typically call on the underscore methods directly, but it’s worth knowing that Python uses methods to handle all of these operators.

Function objects, like other objects, contain double-underscore variables. For example, the documentation for each function is stored in a variable called __doc__:

 >>>​​ ​​import​​ ​​math
 >>>​​ ​​math.sqrt.__doc__
 'sqrt(x) Return the square root of x.'

When we use built-in function print to print that __doc__ string, look what comes out! It looks just like the output from calling built-in function help on math.sqrt:

 >>>​​ ​​print(math.sqrt.__doc__)
 sqrt(x)
 
 Return the square root of x.
 >>>​​ ​​help(math.sqrt)
 Help on built-in function sqrt in module math:
 
 sqrt(...)
  sqrt(x)
 
  Return the square root of x.

Every function object keeps track of its docstring in a special variable called __doc__.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.9