Masking The Built-in Variable With Its Magic Behavior?
Solution 1:
In the interactive prompt, _
has "magic" behavior -- it gets set to the value of whatever expression was evaluated last:
>>> 3 + 3
6
>>> _
6
If, however, you assign something to a variable named _
yourself, then you only "see" that variable, and the magic variable is hidden ("masked"):
>>> _ = 3
>>> 3 + 3
6
>>> _
3
This happens because your local variable _
is unrelated to the variable that has the magic behavior, it just happens to have the same name.
So don't do that, not in the interactive prompt anyway.
Solution 2:
It means exactly what it says it means; you should not assign anything to the _
variable as that would hide the real magic variable:
>>> 1 + 1
2
>>> _
2
>>> _ = 'foo'
>>> 2 + 2
4
>>> _
'foo'
The magic _
variable stores the result of the last expression that was echoed, but by assigning to _
you no longer can 'see' this magic variable. Looking up _
shows whatever value I assigned to it now.
Luckily, you can also delete the shadowing _
name again:
>>> del _
>>> 2 + 2
4
>>> _
4
Post a Comment for "Masking The Built-in Variable With Its Magic Behavior?"