Книга: Practical Programming, Fourth Edition
Назад: Exploring String Methods
Дальше: A Methodical Review

What Are Those Underscores?

Methods whose names begin and end with two underscores are special

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 rarely call these special methods directly, but it is eye-opening to see this, which may help you understand how Python works.

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

 >>>​​ ​​help(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, you can see both versions of getting the absolute value of a number:

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

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

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

Let’s add two integers using this trick:

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

And now let’s compare two numbers to determine which is larger:

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

Dunders

icon indicating an aside

The special methods, such as __init__, __len__, and __str__, are often called dunder (Double-UNDERscore) methods.

Again, programmers don’t typically call 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__
 'Return the square root of x.'

When you 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__)
 Return the square root of x.
 >>>​​ ​​help(math.sqrt)
 Help on built-in function sqrt in module math:
 
 sqrt(x, /)
  Return the square root of x.

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

Назад: Exploring String Methods
Дальше: A Methodical Review