473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

bool v. int distinctions


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?

Jul 18 '05 #1
4 1500
Lee Harr wrote:
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
Jul 18 '05 #2
Lee Harr wrote:

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
Jul 18 '05 #3
>> I am wondering if this is a reasonable thing to do:
[...]
Won't work with 2.1, clearly, but it seems ok in a
recent 2.2 or in 2.3.
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.
True Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'True' is not defined True = 1
True is 1 1python2.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.
True 1 True is 1

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.

Jul 18 '05 #4
>>> I am wondering if this is a reasonable thing to do:
[...]
Won't work with 2.1, clearly, but it seems ok in a
recent 2.2 or in 2.3.
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. True Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'True' is not defined True = 1
Try
True = 1==1


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.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Scott Brady Drummonds | last post by:
Hi, everyone, I have a program in which I need to store a series of Boolean values. A vector<bool> would be ideal. However, I'm concerned about this data structure because of Scott Meyers'...
3
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
4
by: Nomak | last post by:
Hello, With this code: $ cat -n ifs.cc 1 #include <vector> 2 #include <iostream> 3 4 using std::vector; 5 using std::cin;
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
2
by: Jon Shemitz | last post by:
How come I can write code like "if (L)" but NOT code like "if (L == true)" when L is a class with an implicit operator bool? /////////// List L = new List(); public class List { private...
14
by: Chris | last post by:
Hi, I try to print out truth-tables for an &&-operation using the following code, unfortunatly I get compiler errors : for ( byte i1=0; i1<=1; i1++) { for ( byte i2=0; i2<=1; i2++) { bool...
4
by: ORC | last post by:
Is the bool type actually an Int32 with 0 as false and non zero as true? The reason for my question is that I've seen a lot of API calls that return a Int32 implemented in C# as an bool like: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
64
by: shaanxxx | last post by:
I have code which says #define MYBOOL int This code is very old. people who have written is not avaible. I was thinking what could be reason. 1) bool datatype was not available that time (10...
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.