bool v. int distinctions

Pettersen, Bjorn S BjornPettersen at fairisaac.com
Sun Oct 12 10:55:08 EDT 2003


> From: Lee Harr [mailto:missive at frontiernet.net] 
> 
> Hi;
> 
> I am wondering if this is a reasonable thing to do:

No, it unduly restricts the implementation (I forget wheter True/False
are meant to be singletons in all implementation, but I believe not),
and also it is too much typing :-)

> class Pinger:
>     def go(self, repeat=False):
>         if repeat is True:

          if repeat:

>             repeat = -1
>         elif repeat is False:

          else:  # I was actually merrily re-typing your 
                 # version when I discovered what you were 
                 # _really_ doing (not a good sign...)

>             repeat = 1
>         self.repeat = repeat
>         self.ping()
> 
>     def ping(self):
>         while self.repeat:

I would have expected

          while (self.repeat < 0 or self.repeat > 0) is True:

for consistency <wink>? (In any case you might want to add a comment
that self.repeat can be negative, otherwise someone is likely to remove
the if-statement below ;-)

>             print 'ping!'
>             if self.repeat > 0:
>                 self.repeat -= 1
> 
> Won't work with 2.1, clearly, but it seems ok in a
> recent 2.2 or in 2.3.
> 
> I guess my only qualm is that
> 
> p = Pinger()
> p.go(repeat=0)
> 
> may be a bit confusing, since it will not ping at all...
> 
> What do you think?

You're trying to be too clever and/or program in a different language
where conditional and loop expressions must always be a value of type
BOOL (e.g. Pascal/c#). In Python every value (i.e. objects, numbers,
instances, etc.) has a truth-value in addition to their intrinsic value
(just like in e.g. lisp <wink>).. Guido did this very explicitly to
avoid obfuscations like above :-)

-- bjorn





More information about the Python-list mailing list