Recently, I happened to listen to a report on good and bad C programming practices. In particular, it touched upon the topic of decrypting
funny-looking program code (
emoticons in C). Then there was a dispute about the feasibility of using such an intricate code to test the skills of a candidate for the post of a programmer during interviews. The dispute did not lead to a consensus.
Consider a possible question about
emoticons when interviewing for a position implying knowledge of the Python programming language.

Task
There are two expressions:
_+_
_|_
Which of these two expressions will most often lead to an error and why?
Expression NotesIt should be noted that these expressions are poorly read, such expressions are not advisable to use in the working code, but can only be used as a puzzle.
You also need to remember that the
emoticons in the task are not written
pythonically (they do not conform to the
PEP 8 style standard).
Decision
In Python, the underscore is not a
keyword ; therefore, in general, it can be used to assign some variable values.
')
The task is reduced to the consideration of the occurrence of errors in addition and in the case of “bitwise or” (for brevity, simply “or”) one variable. Those. to search for Python built-in types (classes) for which one operation is implemented and to search for built-in types where another operation is implemented.
In the Python console, it's easy to check that there are such underscore values ​​for which:
There is no error in the calculation of each expression. >>> _ = 10 >>> _+_ 20 >>> _ = 10 >>> _|_ 10
There is no error in the calculation of the first expression. >>> _ = '10' >>> _+_ '1010' >>> _ = '10' >>> _|_ Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for |: 'str' and 'str'
There is no error in the calculation of the second expression >>> _ = {1, 0} >>> _+_ Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'set' and 'set' >>> _|_ {0, 1}
In the evaluation of each expression - an error >>> _ = {1: 0} >>> _+_ Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'dict' and 'dict' >>> _|_ Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for |: 'dict' and 'dict'
The magic of Python is that the implementation of additions, and “or”, and other operators is in the so-called
“magic methods” described for the required classes.
For addition this is a method
__add__
For "or" this is the method
__or__
The name of each magic method contains underscores, two at the beginning and end of the corresponding word.
Finding information about built-in Python classes.You can try to find the required information in the documentation for the built-in types on the official Python website:
https://docs.python.org/3.7/library/stdtypes.html .
It is possible to execute in the terminal Python
>>> import builtins >>> help(builtins)
I prefer to use the documentation of exactly the version of Python that I am currently using, referring to the web interface of the pydoc module.
To do this, enter the command line, for example:
python3 -m pydoc -p 3344
and open the browser module documentation
http: // localhost: 3344 / builtins.html .
Built-in classes for which our methods are implemented:
Nine built-in classes implement addition operation and four implement “or”.
Additionally, you can try to speculate about the frequency of using base classes in real projects, about basic classes, and the implementation of the above methods for them. This will probably be positively evaluated during the interview.
Answer 1.The second expression will more often lead to an error.
Possible additional questions about underlining at the interview
What is the role of underscore in object naming?
Answer 2.An underscore can be used to separate words in
different styles of entity naming in Python :
- When naming lowercase
lower_case_with_underscores
- When naming is in upper case
UPPER_CASE_WITH_UNDERSCORES
Some names delimited by underscores are
reserved :
- With an underscore at the beginning of the name is not imported with a general import
from module import *
- With two underscores on both sides of the name is considered systemic, it is not recommended to use such names for other purposes.
- With two underscores, the name is considered private (not public) for the class.
What is the role of a single underscore?
Answer 3.In the interpreter, if the underscore was not explicitly used as a variable, it stores the result of the last non-error action displayed on the screen.
$ python3 Python 3.6.8 (default, Feb 14 2019, 22:09:48) [GCC 7.4.0] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> _ Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name '_' is not defined >>> 2 * 5 10 >>> _ 10 >>> _ = 1 >>> _ 1 >>> 2 * 5 10 >>> _ 1
This is due to the fact that
sys.stdout is escaped using the
displayhook . Take an example from the
official documentation :
def displayhook(value): if value is None: return
In addition, single underlining is used to facilitate
internalization .