Re: None

Tim Peters (
Mon, 25 Apr 94 14:33:40 -0400

> [don]
> ...
> think that it would be nice if None was also a sequence of length zero.
> I have written code that usually returns a list but often the list is
> empty. In this case, I would rather just return None, but doing so
> requires that the caller check for this special case before iterating
> over the result. If None was a sequence of length zero, this would not
> be a problem. Following this reasoning, it would also make sense if
> None was a mapping of length zero.

OTOH, None is very often used now as a kind of "null pointer", and from
that viewpoint it's a feature that using the None object in most contexts
reliably triggers a runtime error. E.g., in your scenario I would like
to reserve a None return value to mean something like "I know I usually
return a list, but in this case I just can't, and you're really screwed
if you called me believing this wasn't a possibility".

So if None didn't exist, we'd have to invent it <wink>.

A new (different) symbol would be OK, though. How about this one?

class _NONEclass:
def __repr__(self):
return 'NONE'
def __len__(self):
return 0
def __getslice__(self,i,j):
return NONE
def keys(self):
return NONE
def items(self):
return NONE

NONE = _NONEclass()

Then "for x in NONE: ...", NONE.keys(), NONE[i:j], etc are well-defined
nops. Add more do-nothing methods to taste.

none-sense-ly y'rs - tim

Tim Peters
not speaking for Kendall Square Research Corp