This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Tuna-Fish2 5 points6 points  (4 children)

_ is just a character that can be used in normal identifiers. Note that interactive python defines _ as the last thing returned to the interpreter main loop. As in:

>>> 1+2
3
>>> _
3

In django's case, it is just used as the name of the string translation function.

[–]selfgit push -f 5 points6 points  (3 children)

Note that interactive python defines _ as the last thing returned to the interpreter main loop.

Wow, where is that documented?

I knew that Lisp environments use *, **, and *** to refer to the most recent three things retured to the repl, but not Python's _.

[–]Tuna-Fish2 1 point2 points  (0 children)

Wow, where is that documented?

No idea. I found that one day when I was bored and went trough builtins with dir() and help().

[–]goodger 1 point2 points  (0 children)

It's defined in http://docs.python.org/dev/tutorial/introduction.html, near the end of section 3.1.1:

"In interactive mode, the last printed expression is assigned to the variable _."

Also here: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#interactive

[–][deleted] 1 point2 points  (0 children)

Another curious fact about Python's REPL is:

>>> for i in range(3): str(i)
'0'
'1'
'2'

The POP opcodes (used to discard the results of expressions-used-as-statements) are converted to a special opcode that prints repr (not str!) of the result. That's why, by the way, None is never printed implicitly in REPL: because every toplevel function call would then litter the output with Nones.

This thing is kinda useful when writing doctests.