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

K

Kristian Domke

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
 
C

cokofreedom

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
 
G

Gary Herron

Kristian said:
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?

Good catch! It's my guess that you've found a way to improve on a bit
of carelessly written code.

However there *is* a (subtle) difference between
not f
and
(not f and 1) or 0

The first produces a boolean value, and the second produces an int
value, but since one is a subclass of the other, you'd have to write
quite perverse code care about the difference.

Gary Herron
 
C

cokofreedom

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! :)
 
J

Jarek Zgoda

Gary Herron napisa³(a):
However there *is* a (subtle) difference between
not f
and
(not f and 1) or 0

The first produces a boolean value, and the second produces an int
value, but since one is a subclass of the other, you'd have to write
quite perverse code care about the difference.

Twisted sems to be perverted to the root.
 
D

Duncan Booth

Kristian Domke said:
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.
 
B

Boris Borcic

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
 
S

Steven D'Aprano

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.


But since Python bools are subclasses from int, both of them are actually
ints. One happens to look like 1, and the other looks like True.
 
G

Gary Herron

Boris said:
I am surprised nobody pointed out explicitely that

True==1 and False==0
Several of us did indeed point this out by saying that bool's are a
subclass of ints.
 
D

Duncan Booth

Steven D'Aprano said:
But since Python bools are subclasses from int, both of them are
actually ints. One happens to look like 1, and the other looks like
True.

The twisted code isn't incorrect, just twisted. :)
 
S

Steven Bethard

Kristian said:
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
 
G

George Sakkis

However there *is* a (subtle) difference between
not f
and
(not f and 1) or 0

The first produces a boolean value, and the second produces an int
value, but since one is a subclass of the other, you'd have to write
quite perverse code care about the difference.

Even if for some reason I did want the result to be int, I would write
it as "int(not f)".

George
 

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

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top