0
Why and when to use "_" or underscore variable in the interpreter ?
2 ответов
+ 1
there are two on either side of magic methods. other than that, they are very useful if you make a function or variable with the same name as a built-in.
For instance, if i had two variables, "from" and "to". I beleive "from" is already a built-in in python. so your variable could be from_ or _from and it wouldnt have any namespace clashes
+ 1
You can read all about the usage of underscore in Python here:
https://dbader.org/blog/meaning-of-underscores-in-python
If you talk about a single underscore, you may want to use it as the loop variable in for loops and comprehensions, when you don't actually care about the value in the loop.
For example, to print "Bam" 3 times, you could write this.
for _ in range(3):
print("Bam")