Why and when to use "_" or underscore variable in the interpreter ? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Why and when to use "_" or underscore variable in the interpreter ?

30th Sep 2020, 4:09 PM
Crypto Genesis
Crypto Genesis - avatar
2 Antworten
+ 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
30th Sep 2020, 4:28 PM
Slick
Slick - avatar
+ 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")
30th Sep 2020, 7:33 PM
Tibor Santa
Tibor Santa - avatar