+ 2

Why symbol (_) is used in python.what does it refer to and when should we use it

1st Jun 2018, 12:33 PM
Ninad Patil
Ninad Patil - avatar
4 odpowiedzi
+ 4
Generally, underscore symbol itself is just a placeholder. It means that if you do not want to use a true variable name to assign an unneeded value to, you can use '_'. Like, if you assign variables with values from a list, this operation is valid (assuming you need only a and b variables to be set) : a, _, b = 1, 99, 3 or a, b, *_ = 1, 5, 7, 99, 56
1st Jun 2018, 1:24 PM
strawdog
strawdog - avatar
+ 1
In classes _ is used to indicate whether an object is public or private. A private object is depicted by, eg: _obj. Private object of a class can't be used outside the class.
1st Jun 2018, 1:39 PM
Astika Nehra
+ 1
thanks guys
2nd Jun 2018, 7:41 AM
Ninad Patil
Ninad Patil - avatar
0
In Python, the _ is generally reserved as a throwaway variable, which merely just indicates that whatever would usually be in its place should be ignored by the interpreter. Another use for it includes holding the place of the most recent input provided by the user Here's a stackoverflow answer to this: https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
1st Jun 2018, 1:18 PM
Faisal
Faisal - avatar