this post was submitted on 08 Jun 2025
522 points (97.5% liked)

Programmer Humor

36460 readers
107 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] ryedaft@sh.itjust.works 4 points 6 days ago (1 children)

You can use anything that doesn't start with a digit or punctuation as a variable name (underscore beginning also allowed) unless it's a keyword.

[–] funkless_eck@sh.itjust.works 3 points 6 days ago* (last edited 6 days ago) (2 children)

_ (sic) as a variable name is often used when a function returns multiple outputs but you only want one

 def my_function:
      return 1, 2, 3

 _, two, _ = my_function()
[–] Archr@lemmy.world 1 points 3 days ago

_ can also be used in the python interactive terminal to mean 'last return value'

Ie:

> 'string'
'string'
> a = _
> print(a)
string
[–] ryedaft@sh.itjust.works 6 points 6 days ago* (last edited 5 days ago)

Underscore alone is a special variable name and I'm pretty sure anything assigned to it goes straight to garbage collection. Whereas _myvariable is typically used to indicate a "private" class variable or method (Python doesn't have private so it's just a convention).