About None value not stored to _ in interactive mode

None (as an expression value) is ignored in REPL. Not displayed (I knew that and it’s documented - everything OK here), but also not stored to the _ variable. I didn’t know and didn’t expect it. I tried to find some explanation and now I think it is not documented well. Did I overlook something or do you also think the documentation is incomplete in this regard?

>>> 1
1
>>> _
1
>>> 2
2
>>> _
2
>>> None
>>> _
2
>>> 

The only related documentation on docs.python.org I could find are these two pieces of information:

“In interactive mode, the last printed expression is assigned to the variable _.” However it is in the “informal introduction” section, so it could be incomplete.

and:

“In interactive mode, if the value is not None, it is converted to a string […] and the resulting string is written to standard output on a line by itself” (source: 7. Simple statements — Python 3.15.2 documentation). This explains why it is not displayed, but not why is it not stored.

The behaviour you’re looking at can actually be customized. What happens is that sys.displayhook is called with the value, and it’s up to that function to choose what to do. If you wanted to, say, have ALL the values retained as __[-1] and __[-2] etc, you could redefine sys.displayhook to do that.

The informal introduction is, as you say, allowed to be a bit incomplete; but I think it would be useful to link to those docs from the section on expression statements. It could be as simple as adding this sentence to the last paragraph:

“This is done by calling sys.displayhook with the value, and can be customized by replacing that function.”

It should be expected behaviour, or you would lose the last result after printing something using print.

I agree; it’s just a question of whether (and where) it’s documented.

For more context this is approximately the origin of my question..

Type "help", "copyright", "credits" or "license" for more information.

>>> [1,2,3].sort()
>>> _ is None
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    _ is None
    ^
NameError: name '_' is not defined

>>> [1,2,3].sort() is None
True

I confirm that I don’t want to propose any change. I was looking for a rationale and I would welcome better documentation as suggested in the accepted solution by Rosuav. I tagged the question with “[documetation]” from the beginning.