mlissner
mlissner

Reputation: 18196

Python convention for variables that won't be used, but are reuturned from a function nonetheless

If I have a function like the following:

def foo():
    return 1, 2

I would normally call the function like:

g, f = foo()

But if I never plan on using the second value returned, is there a way of calling the function that makes that clear, so in the future I won't get distracted by a place-filler variable?

For example, something like:

g, not_used = foo()

Seems like there's probably a standard way to do this that is out there.

Upvotes: 1

Views: 281

Answers (2)

Brendan Long
Brendan Long

Reputation: 54272

You could get the first item directly:

g = foo()[0]

I think pylint recommends _ or dummy:

g, _ = foo()

Upvotes: 6

Adam Wagner
Adam Wagner

Reputation: 16117

I usually just ask for the index of the value I care about

g = foo()[0]

Upvotes: 1

Related Questions