Skip to content

gh-85566: State interpreter var "_" in appendix of tutorial #21654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

wyz23x2
Copy link
Contributor

@wyz23x2 wyz23x2 commented Jul 28, 2020

Continue of the not-working #21651 .
This patch states the special variable _ in interactive mode and also includes an example.

@bedevere-bot bedevere-bot added docs Documentation in the Doc dir awaiting review labels Jul 28, 2020
@wyz23x2 wyz23x2 changed the title bpo-41394: State special _ in appendix of tutorial bpo-41394: State interpreter var "_" in appendix of tutorial Jul 28, 2020
@wyz23x2
Copy link
Contributor Author

wyz23x2 commented Jul 28, 2020

Updated.

@wyz23x2
Copy link
Contributor Author

wyz23x2 commented Jul 28, 2020

There is a bug in Travis CI.

Failed example:
    _  # Not defined yet
Expected:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
        _
    NameError: name '_' is not defined
Got:
    369

Uh... That must be because the prev. process return value in the bot is 369, but it's a new interpreter in the example.

@wyz23x2
Copy link
Contributor Author

wyz23x2 commented Jul 28, 2020

Same in doc check ╮(╯▽╰)╭

Failed example:
    _  # Not defined yet
Expected:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
        _
    NameError: name '_' is not defined
Got:
    time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

That must be the recent value too.

@wyz23x2
Copy link
Contributor Author

wyz23x2 commented Jul 28, 2020

IMO the doctest should be done in a separate namespace. What if it's something like __import__('os').system('rm -rf /*')?

Comment on lines +39 to +40
There is a special variable ``_`` in interactive mode, which stores the result of the last
evaluated expression. If the last expression evaluated to ``None``, then the value of ``_``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking the two commas here are somewhat superfluous. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some grepping:

sk@note:~/src/cpython $ git grep '[[:space:]]`None' Doc/  # no hits
sk@note:~/src/cpython $ git grep -F '``None``' Doc/|wc -l
1396

@gvanrossum
Copy link
Member

Re: doctest failures. We trust devs not to put attacks in doctests (honestly, if we can't trust devs there are a lot of other possible attack vectors :-). For contributors we trust reviewers to review the code before running it.

So about how to unbreak this, I think there's a way to insert something in the source file that is executed but not displayed. Look here: https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html (testsetup)

@MaxwellDupre
Copy link
Contributor

Not really part of Error handling, hence should be a new section.

@gvanrossum
Copy link
Member

@rhettinger Can you help this forward?

@gvanrossum gvanrossum changed the title bpo-41394: State interpreter var "_" in appendix of tutorial gh-85566: State interpreter var "_" in appendix of tutorial Dec 7, 2022
@gvanrossum
Copy link
Member

@rhettinger ^^

@furkanonder
Copy link
Contributor

cc: @merwok

@python-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

CLA signed

@skirpichev skirpichev self-requested a review April 18, 2025 12:48
Comment on lines +66 to +71

>>> _ # Not defined yet
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_
NameError: name '_' is not defined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> _ # Not defined yet
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_
NameError: name '_' is not defined
.. doctest::
:options: +SKIP
>>> _ # Not defined yet
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_
NameError: name '_' is not defined

I doubt this example is fixable. Either you could remove one or exclude entire block from testing, as suggested.

Comment on lines +61 to +63
There is a special variable ``_`` in interactive mode, which stores the result of the last
evaluated expression. If the last expression evaluated to ``None``, then the value of ``_``
is not updated.
Copy link
Contributor

@skirpichev skirpichev Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
There is a special variable ``_`` in interactive mode, which stores the result of the last
evaluated expression. If the last expression evaluated to ``None``, then the value of ``_``
is not updated.
There is a special variable ``_`` in interactive mode, which is updated with the result
of the last evaluated expression, unless it was ``None``.

Copy link
Member

@merwok merwok Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the verb should be in the past: unless it was None

but would like a native speaker to confirm!

Comment on lines +1 to +3
Added "Special ``_`` Variable" section to the "16. Appendix" part of Python tutorial.

It includes the define of ``_`` and an example.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Added "Special ``_`` Variable" section to the "16. Appendix" part of Python tutorial.
It includes the define of ``_`` and an example.
Added "Special ``_`` Variable" section to the "16. Appendix" part of Python tutorial.

@@ -53,6 +53,32 @@ Typing an interrupt while a command is executing raises the
:exc:`KeyboardInterrupt` exception, which may be handled by a :keyword:`try`
statement.

.. _tut-var:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about something less generic / cryptic:

Suggested change
.. _tut-var:
.. _tut-underscore-variable:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.