bool v. int distinctions

L

Lee Harr

Hi;

I am wondering if this is a reasonable thing to do:


class Pinger:
def go(self, repeat=False):
if repeat is True:
repeat = -1
elif repeat is False:
repeat = 1
self.repeat = repeat
self.ping()

def ping(self):
while self.repeat:
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?
 
M

Michael Geary

Lee said:
I am wondering if this is a reasonable thing to do:

class Pinger:
def go(self, repeat=False):
if repeat is True:
repeat = -1
elif repeat is False:
repeat = 1
self.repeat = repeat
self.ping()

def ping(self):
while self.repeat:
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?

I think the specification and test cases are missing. :)

It is a bit unclear what you intend the code to do. What parameter types and
values can be passed to Pinger.go()? Is Pinger.ping() intended to be called
externally, or is it just an internal method used by Pinger.go()? Why is
this a class at all instead of just a function?

If you write out an exact specification, and write the test cases to go with
that, then it should become clear whether it's confusing or not.

-Mike
 
P

Peter Otten

Lee said:
Hi;

I am wondering if this is a reasonable thing to do:


class Pinger:
def go(self, repeat=False):
if repeat is True:
repeat = -1
elif repeat is False:
repeat = 1
self.repeat = repeat
self.ping()

def ping(self):
while self.repeat:
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?

I think it's a bad idea to mix boolean and integer values. It obfuscates the
logic, and many libraries still use 0 as False and 1 as True.
Also, I would separate repetition from action more clearly:

FOREVER = -1
class Pinger2:
def repeat(self, count=1):
assert count >= 0 or count == FOREVER, "count must be >=0 or
FOREVER"
if count == FOREVER:
while True:
self.ping()
else:
for i in range(count):
self.ping()

def ping(self):
print 'ping!'

You could even go further and change repeat() into a function that takes an
additional callable oject:

def repeat(self, count, action)

Peter
 
L

Lee Harr

I am wondering if this is a reasonable thing to do:
[...]
What makes you think it won't work in 2.1?

python2.1
Python 2.1.3 (#1, Apr 19 2003, 09:07:31)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File said:
python2.2
Python 2.2.3 (#1, Sep 26 2003, 13:20:35)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.0


Anyhow, I think you are all right. It was a bad idea.

I ended up using count instead of repeat which makes way more
sense and completely sidesteps this whole issue.

Thanks for your time.
 
L

Lee Harr

I am wondering if this is a reasonable thing to do:
[...]
What makes you think it won't work in 2.1?
python2.1
Python 2.1.3 (#1, Apr 19 2003, 09:07:31)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "copyright", "credits" or "license" for more information.Traceback (most recent call last):

to get a boolean true instead of an integer true :)


Oh, nice. Not that I use 2.1 for anything except Zope anymore...
but still very interesting.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top