0

At 44:05 in his Fun of Reinvention talk, Dave Beasley writes

>>> d = _

There is a lot before that, which is necessary for the result he gets. But ignoring the output, what does that input line mean? Whenever I try it, either in a file in the PyCharm editor, in the PyCharm Python console, using straight IDLE (all v3.7) I get an error.

Any idea what this may mean and how to get something like that to run?

Thanks

RussAbbott
  • 2,660
  • 4
  • 24
  • 37
  • Did you make sure to run the stuff before that first? You can't just execute one line out of context and expect it to work. – user2357112 Apr 12 '19 at 17:06
  • Yes, I ran all the stuff before that. But I ran it in a file, not line-by-line in IDLE. The result is different! Thanks to the link above and the answer below, I now see it's a special symbol in a REPL like IDLE. – RussAbbott Apr 12 '19 at 17:46

1 Answers1

2

_ is a special variable in the python language.

In some REPLs, like IDLE, it holds the result of the last expression executed.

d = _ assigns the result of the last expression executed to d.

rdas
  • 20,604
  • 6
  • 33
  • 46
  • 1
    This is *only true in a repl*. It has no special status other than that, and other interactive repls (IPython) have other such "special" variables. – juanpa.arrivillaga Apr 12 '19 at 17:15
  • I've edited in a clarification. Thanks! – rdas Apr 12 '19 at 17:17
  • Thanks. I wasn't aware of the IDLE use to refer to the previous result. I tried it in a file with all the previous stuff, but in that context, it doesn't mean the same thing! – RussAbbott Apr 12 '19 at 17:44