boolean from a function

A

Andrea Crotti

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...
 
G

Grant Edwards

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".
 
S

Steven D'Aprano

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 :)
 
A

Andrea Crotti

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..
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top