bool v. int distinctions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lee Harr

    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?

  • Michael Geary

    #2
    Re: bool v. int distinctions

    Lee Harr wrote:[color=blue]
    > 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?[/color]

    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


    Comment

    • Peter Otten

      #3
      Re: bool v. int distinctions

      Lee Harr wrote:
      [color=blue]
      >
      > 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?[/color]

      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

      Comment

      • Lee Harr

        #4
        Re: bool v. int distinctions

        >> I am wondering if this is a reasonable thing to do:
        [...][color=blue][color=green]
        >> Won't work with 2.1, clearly, but it seems ok in a
        >> recent 2.2 or in 2.3.[/color][/color]
        [color=blue]
        > What makes you think it won't work in 2.1?[/color]

        [color=blue]
        >python2.1[/color]
        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.[color=blue][color=green][color=darkred]
        >>> True[/color][/color][/color]
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        NameError: name 'True' is not defined[color=blue][color=green][color=darkred]
        >>> True = 1
        >>> True is 1[/color][/color][/color]
        1[color=blue][color=green][color=darkred]
        >>>[/color][/color]
        >python2.2[/color]
        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.[color=blue][color=green][color=darkred]
        >>> True[/color][/color][/color]
        1[color=blue][color=green][color=darkred]
        >>> True is 1[/color][/color][/color]
        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.

        Comment

        • Lee Harr

          #5
          Re: bool v. int distinctions

          >>> I am wondering if this is a reasonable thing to do:[color=blue]
          > [...][color=green][color=darkred]
          >>> Won't work with 2.1, clearly, but it seems ok in a
          >>> recent 2.2 or in 2.3.[/color][/color][/color]
          [color=blue][color=green]
          >> What makes you think it won't work in 2.1?[/color][/color]

          [color=blue][color=green]
          >> python2.1[/color]
          > 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.[color=green][color=darkred]
          > >>> True[/color][/color]
          > Traceback (most recent call last):
          > File "<stdin>", line 1, in ?
          > NameError: name 'True' is not defined[color=green][color=darkred]
          > >>> True = 1[/color][/color][/color]
          [color=blue]
          >Try
          >[color=green][color=darkred]
          > >>> True = 1==1[/color][/color]
          >
          >to get a boolean true instead of an integer true :-)
          >[/color]


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

          Thanks.



          Comment

          Working...