tags:

views:

89

answers:

2
+5  Q: 

Assignment to None

Hello,

I have a function which returns 3 numbers, e.g.:

def numbers():
   return 1,2,3

usually I call this function to receive all three returned numbers e.g.:

a,b,c=numbers()

However, I have one case in which I only need the first returned number. I tried using:

a, None None = numbers()

But I receive "SyntaxError: assignment to None".

I know, of course, that i can use the first option I mentioned and then not use "b" and "c", but only "a". However, this seems like a "waste" of two vars and feels like wrong programming.

Any ideas?

Thanks,

Joek

+12  A: 
a, _, _ = numbers()

is a pythonic way to do this. you could also use:

a, *_ = numbers()

if your version of Python supports it.

To clarify _ is a normal variable name in Python, except it is conventionally used to refer to non-important variables.

SilentGhost
geez that was fast! :)Thanks!
Joel
Except when it refers to something in `gettext`, or is used as the tally variable in the REPL.
Ignacio Vazquez-Abrams
+5  A: 

Another way is of course a=numbers()[0], if you do not want to declare another variable. Having said this though, I generally use _ myself.

Nikwin