2

Let's say that I have function foo that returns a single output. I do not make use of that output in the caller of the function foo.

What is the most elegant way to handle this?

r = foo()
# r is not used anywhere later.

Or should I just not do any assignment when calling foo()? The only thing is that I probably get lint warning if I do this.

foo()
....
leopoodle
  • 2,110
  • 7
  • 24
  • 36

1 Answers1

4

Just ignore the output:

foo()

Another option (which personally, I don't like too much) is assign the output to a variable '_':

_ = foo()

This is a conventional way to say that the returned value is ignored / unneeded, and is usually used in for loops:

lst = [1, 2, 3]
for _ in lst:
    # do something unrelated to the current element in the list
    print 'hi'
asherbret
  • 5,439
  • 4
  • 38
  • 58
  • The first and third code are okay. The 2nd you stated " I don't like too much" which I agree with and don't think its even worth mentioning (unless unpacking multiple variables eg. `a, _ = items`. However also keep in mind even the use of `_` is no longer recommended because it can conflict with the django translation `_` – jamylak Oct 18 '16 at 06:32
  • I only mentioned option #2 because OP said option #1 would "probably get lint warning". In case that turned out to be true, I gave the 2nd option. The 3rd code is there to show where option #2 is usually used. In any case, I was unaware `_` had special meaning in `Django`. I'll be sure to look into that. Thank you. – asherbret Oct 18 '16 at 06:47