471,082 Members | 706 Online

# Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'?

Hello to all

I am trying to learn python at the moment studying an example program
(cftp.py from the twisted framework, if you want to know)

There I found a line

foo = (not f and 1) or 0

In this case f may be None or a string.

If I am not wrong here, one could simply write

foo = not f

because if f = None:

(not f) = true,
(true and 1) = true,
(true or 0) = true

or if f = 'bar'

(not f) = false
(false and 1) = false
(false or 0) = false

So why bothering with the longer version?

I hope, I made clear, what I want...

CU

Kristian
Jan 23 '08 #1
5 985
On Jan 23, 9:45 am, Kristian Domke <n...@neither-nor.netwrote:
Hello to all

I am trying to learn python at the moment studying an example program
(cftp.py from the twisted framework, if you want to know)

There I found a line

foo = (not f and 1) or 0

In this case f may be None or a string.

If I am not wrong here, one could simply write

foo = not f

because if f = None:

(not f) = true,
(true and 1) = true,
(true or 0) = true

or if f = 'bar'

(not f) = false
(false and 1) = false
(false or 0) = false

So why bothering with the longer version?

I hope, I made clear, what I want...

CU

Kristian
f = None
foo = (not f and 1) or 0
# this gives you 1

f = None
foo = not f
# this gives you True

Jan 23 '08 #2
Sorry, posted to quickly.

Yes your logic is correct about the "logic" of the return, but theirs
actually differs in what it returns, and I am guessing it is an
important change. Where is this "foo" used? Perhaps its value is used
in a way a boolean return couldn't be?

Just a note, with these kind of points it is often worth just opening
up a python console and printing out the results. Though I do agree
with you, at first look it seems almost redundant to use 1 and 0.
Personally haven't seen this before, rather like it! :)
Jan 23 '08 #3
Kristian Domke <ne**@neither-nor.netwrote:
foo = (not f and 1) or 0

In this case f may be None or a string.

If I am not wrong here, one could simply write

foo = not f
Yes, it sounds pretty silly, and not just on the level you spotted.

The only difference between the two expressions is that the original sets
foo to an integer whereas your version sets it to a bool. So the question
of which is most appropriate actually comes down to what foo is being used
for.

Is there really some code which requires a numeric value of 1 when f is
None or an empty string and a value of 0 for any other string? I can't
think offhand of any obvious situations where you would want that. My guess
is that foo is being used later as a condition in an 'if' statement.

If you really do need an integer then in Python 2.5+ another way to write
it would be:

foo = 0 if f else 1

Also 'foo' is a silly name since it gives no indication at about the
purpose of the expression, but I'm hoping that was just you paraphrasing
the code you posted.

Ok, I just looked at the code, it is indeed being used as a boolean, so

self.useProgressBar = not f
or
self.useProgressBar = f is not None

if you want to be more specific about checking for None.
Jan 23 '08 #4

I am surprised nobody pointed out explicitely that

True==1 and False==0

so that for instance

5*(True+True)==10

and even (but implementation-dependent) :

5*(True+True) is 10

BB

Jan 23 '08 #5
Kristian Domke wrote:
I am trying to learn python at the moment studying an example program
(cftp.py from the twisted framework, if you want to know)

There I found a line

foo = (not f and 1) or 0
Equivalent to ``foo = int(not f)``
In this case f may be None or a string.

If I am not wrong here, one could simply write

foo = not f
No cast to int() here.

STeVe
Jan 23 '08 #6

### This discussion thread is closed

Replies have been disabled for this discussion.