Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17147 > unrolled thread

boolean from a function

Started byAndrea Crotti <andrea.crotti.0@gmail.com>
First post2011-12-13 16:24 +0000
Last post2011-12-14 09:42 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  boolean from a function Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-12-13 16:24 +0000
    Re: boolean from a function Grant Edwards <invalid@invalid.invalid> - 2011-12-13 17:20 +0000
    Re: boolean from a function Duncan Booth <duncan.booth@invalid.invalid> - 2011-12-13 17:42 +0000
    Re: boolean from a function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-13 23:37 +0000
      Re: boolean from a function Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-12-14 09:42 +0000

#17147 — boolean from a function

FromAndrea Crotti <andrea.crotti.0@gmail.com>
Date2011-12-13 16:24 +0000
Subjectboolean from a function
Message-ID<mailman.3604.1323793451.27778.python-list@python.org>
I'm not sure for how long I had this bug, and I could not understand the 
problem.

I had a function which would return a boolean

def func_bool():
     if x:
         return True
     else: return False

Now somewhere else I had

if func_bool:
     # do something

I could not quite understand why it was always true, until I finally noticed
that the () were missing.
Is there some tool to avoid these stupid mistakes? (pylint doesn't warn 
me on that)
I don't think I will ever (or almost) have to use a function as a 
boolean, instead of its return value...

[toc] | [next] | [standalone]


#17154

FromGrant Edwards <invalid@invalid.invalid>
Date2011-12-13 17:20 +0000
Message-ID<jc81h3$k31$1@reader1.panix.com>
In reply to#17147
On 2011-12-13, Andrea Crotti <andrea.crotti.0@gmail.com> wrote:

> Now somewhere else I had
>
> if func_bool:
>      # do something
>
> I could not quite understand why it was always true, until I finally
> noticed that the () were missing. Is there some tool to avoid these
> stupid mistakes? (pylint doesn't warn me on that) I don't think I
> will ever (or almost) have to use a function as a boolean, instead of
> its return value...

FWIW, I have do use the truth value of a function (rather than it's
return value) when writing code that uses callbacks:

def foo(callback=None):
    # do some stuff
    if callback:
        callback()
    # do some more stuff        

It probably would be better to use "if callback is not None:", but I
find I usually skip "is not None".
    
-- 
Grant Edwards               grant.b.edwards        Yow! Will it improve my
                                  at               CASH FLOW?
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#17157

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2011-12-13 17:42 +0000
Message-ID<Xns9FBAB42EB58B0duncanbooth@127.0.0.1>
In reply to#17147
Andrea Crotti <andrea.crotti.0@gmail.com> wrote:

> I'm not sure for how long I had this bug, and I could not understand
> the problem.
> 
> I had a function which would return a boolean
> 
> def func_bool():
>      if x:
>          return True
>      else: return False
> 
> Now somewhere else I had
> 
> if func_bool:
>      # do something
> 
> I could not quite understand why it was always true, until I finally
> noticed that the () were missing.
> Is there some tool to avoid these stupid mistakes? (pylint doesn't
> warn me on that)
> I don't think I will ever (or almost) have to use a function as a 
> boolean, instead of its return value...

For this particular example why don't you just write 'if x: # do 
something'?

More generally rather than having a global function make it a property on 
an object.

    class Snark:
        def __init__(self, something):
            self.boojum = ...

        @property
        def is_a_bookum(self):
           return self.boojum

    hunted = Snark(whatever)


Then you can write:

    if hunted.is_a_boojum:
        self.vanish_away()

-- 
Duncan Booth http://kupuguy.blogspot.com

[toc] | [prev] | [next] | [standalone]


#17177

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-13 23:37 +0000
Message-ID<4ee7e1a4$0$29979$c3e8da3$5496439d@news.astraweb.com>
In reply to#17147
On Tue, 13 Dec 2011 16:24:05 +0000, Andrea Crotti wrote:

> I'm not sure for how long I had this bug, and I could not understand the
> problem.
> 
> I had a function which would return a boolean
> 
> def func_bool():
>      if x:
>          return True
>      else: return False

x is a global? Poor design. But in any case, instead of an explicit 
if...else block, the canonical way to convert an arbitrary object to True/
False is with bool:

def func_bool():
    return bool(x)

But you don't need it. See below.


> Now somewhere else I had
> 
> if func_bool:
>      # do something

That would be better written as:

if x:
   ...

since func_bool always refers to x, it is just a needless level of 
indirection that doesn't buy you anything.


> I could not quite understand why it was always true, until I finally
> noticed that the () were missing.
> Is there some tool to avoid these stupid mistakes? (pylint doesn't warn
> me on that)

Can't help you with that.

Have you tried pychecker? I can't help you with that either :)



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#17188

FromAndrea Crotti <andrea.crotti.0@gmail.com>
Date2011-12-14 09:42 +0000
Message-ID<mailman.3629.1323855732.27778.python-list@python.org>
In reply to#17177
On 12/13/2011 11:37 PM, Steven D'Aprano wrote:
>
> x is a global? Poor design. But in any case, instead of an explicit
> if...else block, the canonical way to convert an arbitrary object to True/
> False is with bool:
>
> def func_bool():
>      return bool(x)
>
> But you don't need it. See below.
>
>
>
No no it was just to show the pattern, it wasn't the actual code.
I don't like to have useless indirections, so I wouldn't do that...

I like the idea of the property (from Duncan Booth) but the thing is 
that that function
looks like it's doing something (from its name), it's not just a simple 
property.

In the case of the square

class Sq(object):
      def __init__(self, x, y):
            self.x = x
            self.y


It makes perfect sense to have "area" as a property, because you can 
either compute
it and cache it or compute it on demand.
It feels a bit less natural to create a property on something that is 
less simple than that imho..

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web