473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 1027
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
Why is it ambiguous? ------ foo.cpp ------ struct Foo { Foo operator* (Foo) { return Foo(); } Foo operator* (int) const { return Foo(); } Foo () {} Foo (int) {} };
10
by: Simon Adler | last post by:
Hello, in the last time I see a lot like: float * foo = new float; First question: This isn't right, isn't it? It has to be: float ** foo = new float; ?????
30
by: Mason A. Clark | last post by:
This seems too simple by I can't find the answer. Must <a name="foo"> be closed with </a> ? Must it have content? W3C says the content cannot be block so what if : <a name="foo"> <h2>Foo...
9
by: Koster | last post by:
Something I've been puzzled over for a while. This really has no consequence at all, but I'm curious for the _reason_ behind where we commonly place the asterisk when declaring pointer variables....
9
by: C. J. Clegg | last post by:
When you say "const int FOO = 0" (as the commonly-recommended C++ alternative to "#define FOO 0"), isn't that declaration globally visible? I have "const int FOO = 0;" in one source file and...
2
by: Ray Tayek | last post by:
hi, trying to make an array of function pointers to make delegates with. but the compiler does not like: void (Foo::^p)()=&Foo::bar; i did find an article that showed how to convert a delegate to...
14
by: António Marques | last post by:
Hi! I don't think I've ever been here, so I assume I'm addressing a bunch of nice people. Can you help me? I don't think there's a solution, but who knows. The thing is, picture a large...
2
by: Jim Langston | last post by:
In my function/method paramater list I try to keep constant correctness. I.E. I would use: int Foo( const char* a, int b ) instead of int Foo( char* a, int b ) My question reguards int b. ...
2
by: blackholebutterfly | last post by:
Why isn't this allowed? function Foo(){ this.param1 = 'Constructor Function Foo'; } var foo = new Foo; var newfoo = new foo; The last line above gives an error. I thought all objects...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.