6
- What this
_
ago? - Why it is being used in the code snippet below?
print('3 numeros')
data = []
for _ in range(3):
data.append(input())
numbers = list(map(int, data))
print(numbers)
6
_
ago? print('3 numeros')
data = []
for _ in range(3):
data.append(input())
numbers = list(map(int, data))
print(numbers)
8
The _
suggests by convention that wants to discard the value that would normally be stored in a variable. In this case you are taking a track counting up to 3, but you do not want to use this value for anything, this construction is only made to control the repetition 3 times. It’s not idiomatic, but it would be basically the same as writing:
print('3 numeros')
data = []
i = 0
while i < 3:
data.append(input())
i += 1
numbers = list(map(int, data))
print(numbers)
I could have written like that:
print('3 numeros')
data = []
for i in range(3):
data.append(input())
numbers = list(map(int, data))
print(numbers)
I put in the Github for future reference.
It gives in anyway, but gives the impression that this variable will be used for something, the convention is that _
would not be used, although technically this is a valid variable name, so it is only convention itself, not an operator or something special in language.
I would highlight the last sentence: it’s just a convention.
In python _
is a variable and not an operator? For example in x, _, y = (1, 2, 3)
or x, *_, y = (1, 2, 3, 4, 5)
would not be the _
an operator, in that context, specific to the disposal?
@Augustovasques I’m going to highlight the passage quoted by Anderson, but that’s what’s in the answer, isn’t it? You want me to make that clear too?
Actually I was unaware of the concept in question, that the _
It’s just a variable, and I just asked for clarification from someone who mastered the subject. Perhaps my mistake was that I was too interested in the topic and wanted to know more and presented another context, which was not the focus of the question, seeking to know if the same concept extends to other cases of use of _
.
@Augustovasques It is the same case. The _
will be a variable with the value 2; you can use it normally (https://ideone.com/eHGB6x). The point is that, given the convention, you inform the reader that the second value will not be useful to him in the next lines and that he will not need to worry about it.
-1
The underscore prefix serves as a hint to another programmer that the variable or method starting with a single underscore is intended for internal use. This convention is defined in PEP 8. This is not imposed by Python. Python has no distinctions between "private" and "public" variables, such as in Java
The language is python. And operator _
appears in five contexts within the language: Store the last interpreted result, ignore specific values, special meanings, internationalization functions and digit separation. The user was specific in the question about which context the operator uses and this answer does not apply.
Browser other questions tagged python list variables for
You are not signed in. Login or sign up in order to post.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero