Boolean Values
Moshe Zadka
moshez at zadka.site.co.il
Tue Apr 3 15:59:32 EDT 2001
For all those crying for boolean values, here's a simple module
that will hopefully solve all your needs:
=========== Boolean.py ==============
# Boolean.py
class _true:
def __nonzero__(self):
return 1
class _false:
def __nonzero__(self):
return 0
class _dunno:
def __nonzero__(self):
raise ValueError("I dunno")
import __builtin__
__builtin__.true = _true()
__builtin__.false = _false()
__builtin__.dunno = _dunno()
=========== /Boolean.py ==============
Use as follows:
Python 2.1b2 (#1, Mar 24 2001, 09:04:30)
[GCC 2.95.3 20010219 (prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import Boolean
>>> if true:
... print "yay!"
...
yay!
>>> if false:
... print "wrong"
...
>>> if dunno:
... print "huh?"
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "Boolean.py", line 12, in __nonzero__
raise ValueError("I dunno")
ValueError: I dunno
To the people who don't know me yet: when I say "it can be done in
Python in 20 lines, here's the code", I usually mean "I don't think
it has any merit. Nobody's done it not because it's hard, but because
it's useless" ;-)
I'm only clarifying lest someone will actually try to get this integrated
into Python ;-)
--
"I'll be ex-DPL soon anyway so I'm |LUKE: Is Perl better than Python?
looking for someplace else to grab power."|YODA: No...no... no. Quicker,
-- Wichert Akkerman (on debian-private)| easier, more seductive.
For public key, finger moshez at debian.org |http://www.{python,debian,gnu}.org
More information about the Python-list
mailing list