Hacker News new | ask | show | jobs
by aorist 1063 days ago
> The underscore _ can be used as a throwaway variable to discard unwanted values:

So can any other variable, using underscore is just a convention to make it obvious that you're not planning to re-use it (it doesn't get GCed more aggressively or anything).

Similarly, private methods being prefixed with an underscore is also just a convention, you can access them from anywhere.

However, double underscores are used for magic attributes and name mangling for class attributes, which are interpreted differently! (See: https://stackoverflow.com/a/1301369)

2 comments

Many linters are also configured to ignore '_' for many tests (such as any 'unused variable' warnings)
In the interactive environment _ automatically holds the value of the last statement executed.

  >>> 1+1
  2
  >>> _ * 3
  6