Assigning to None

Rocco Moretti roccomoretti at hotpop.com
Fri Jul 1 15:08:01 EDT 2005


Mike Meyer wrote:
> 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)

Goodness, that's bad form. I can just see someone copying it and doing:

(foo, bar, None) = gen_tuple(stuff)
if foo is None:
      ......

and wondering why the program doesn't work properly.

If you had to do that, you might try:

foo, bar, _ = gen_tuple(stuff)

or in a more complex case.

foo, _, _, bar, _, baz, _ = gen_tuple(stuff)

as '_' is already special cased (last result in interactive mode), and 
is already used for "don't care" sematics in Prolog.



More information about the Python-list mailing list