PEP 285: Adding a bool type

Alex Martelli aleax at aleax.it
Tue Apr 2 17:01:57 EST 2002


Chris Liechti wrote:
        ...
>> Thinking in terms of something vs. nothing is an excellent way to
>> explain how Python treats these things, but I still have a problem with
>> it. Who is to say that an empty list is nothing?
> 
> you can guide that in your classes with __nonzero__ (or __len__). if you

You can do that (subclass list and add __nonzero__ -- don't muck
with __len__, of course) but it's hard to see what you will use to decide
that your list "is something" or "is nothing", except the issue of whether
it has or lack contents.

> don't think of 'find' as a boolean operation. its result is an index, not
> a truth value. maybe it would be clearer if it returns None or raises an
> exception if nothing was found but its as it is now (the strstr C function

Guido's time machine has beaten you to this one...:

>>> 'carciofo'.index('cio')
3
>>> 'carciofo'.index('ciao')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: substring not found in string.index

If you want an exception on failure, somestring.index already does it, see.
somestring.find is just like somestring.index BUT doesn't raise an 
exception on failure -- rather, it returns -1 in that case.

However, Python doesn't use "success" (no exception) or "failure"
(exception) to steer its if -- I've already suggested looking into Icon,
which DOES, but you'll never make Python into Icon, of course.

> yes. but a real bool could be introduced as module for now (for the RPC
> and marshalling people that need one) i think its not needed otherwise

A standard library module sounds good.  Or maybe it could go into operator.


Alex




More information about the Python-list mailing list