Link to home
Start Free TrialLog in
Avatar of chima
chima

asked on

Python variable _ manually assigned

Hello,
If the system variable, i.e., _  that is, "_" is manually assigned, which is a no, no; how then would I remove the "independent local variable?"
Thanks
Avatar of James Bilous
James Bilous
Flag of United States of America image

Are you looking for the "del" function?

del _

Open in new window

Avatar of chima
chima

ASKER

James, thank you.  If I del _ does this keep the "built in" _ variable?
Correct, it deletes the local assignment
Your _ is not a system variable, nor built-in variable in Python. It is the same as, say, the a variable. Actually, it may be considered rather confusing, because it is not too visible and... looks strange.

A side note: The underscore was originally introduced for underlining on a mechanical typewriter. The later use was to replace space in longer identifiers.
pepr you are correct, but _ does have special meaning in python. You can use _ when you do not care about the assignment of something, such as a "for in" loop. It also contains the result of the last executed statement. I'm wondering if this is what chima means by system variable.
@James: No, the _ does not have any special meaning in Python. The only special role of underscore in Python of underscore is when two underscores are used as a prefix of class members. And even this is used rarely (unless you really care to make them "private").

It also does NOT contain result of the last executed statement. It is from another language.

Actually, you should avoid using the _ as any identifier. It makes your source less readable.
ASKER CERTIFIED SOLUTION
Avatar of James Bilous
James Bilous
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chima

ASKER

James I agree with you and yes this is what I was asking about.  Thank you
For the interactive environment, I agree. On the other hand, for the interactive environment, it is written using the core of Python, and the _ is just a variable for the interactive environment. It does not mean that you should use such variable for your own script. Actually, you almost cannot use it for your own purpose in the interactive environment. But this does not mean that you should use it in non-interactive mode :) Here the i does the same, plust the _ refers to the i object.

>>> for i in range(1, 5):
...     print(i)
...
1
2
3
4
>>> i
4
>>> _
4

Open in new window

But almost cannot use it because it changes, and you do not know how.