Assigning to None

Roy Smith roy at panix.com
Fri Jul 1 14:47:04 EDT 2005


>Peter Hansen <peter at engcorp.com> writes:
>
>> Mike Meyer wrote:
>>> Yes. I once grabbed an old program that did assignments to None. But
>>> that's always been a bad idea.
>> What was the use case!?
>
>Unpacking a tuple. Something like this:
>
>          (foo, bar, None) = gen_tuple(stuff)

I can see several reasonable ways to avoid that.

1) If you wrote gen_tuple(), and know you don't need the third element
   of the tuple, just change gen_tuple() to only return the first two!
   Or, invent a little data class for it to return.  If you can't do
   that (for whatever reason), then...

2) foo, bar, baz = gen_tuple(stuff), where foo, bar, and baz are all
   meaningful names.

3) foo, bar, dummy = gen_tuple(stuff), to emphasize that the last value
   is ignored.

4) foo, bar = gen_tuple(stuff)[0:1].  In some ways, this is the
   cleanest because it doesn't pollute the namespace with an un-needed
   variable, but I think it's the least readable.





More information about the Python-list mailing list