Super simple python questions: how do you do (!x) and '? x:y'
I can't believe I'm actually asking this question, but I can't seem to find an answer (granted, I haven't looked *that* hard).
How can I make this work the way I want it to? Code:
foo = False edit: While I'm asking, how about this: Code:
maxLen = (maxLen > len(bar)) ? maxLen : len(bar) |
You can try this!
Code:
foo = False Scott |
Quote:
Quote:
|
you have already assigned 'false' to 'foo'
Code:
foo = False |
Quote:
The question is how to test for false? In C/C++, php, java, perl(?), etc, you can test for this by using the '!' logical operator (i.e. (!False == True) evaluates to True), but python doesn't seem to have that - at least not by itself... it appears you have to use '!='. Is that correct? Is there a way of doing this test without using "(foo != True)"? ... and just to clarify, in python, "False" is a keyword, "false" is not. Maybe I should change my example to print out "foo is false" rather than just "false" |
really? it prints "False" on my rig.
Code:
if not foo: |
Quote:
... and clearly, that's a round about way of doing things - the actual code in my program has more meaning, but this was the simplest example to post. ;) Thanks again! If anyone knows a method for acheiving my second example, feel free to chime in. |
I think (at least initially) the python design philosophy was to keep the language simple by only having only one way to express each construct. So if/then/else/elif was supposed to be enough without ?/: and switch statements. In this situation you can also (rather artificially) use a dictionary:
maxLen = {True: maxLen, False: len(bar)}[maxLen > len(bar)] although this trick is more useful for making switch type statements. Of course with your example the simplest form is maxLen = max(maxLen, len(bar)) although I don't think that's what you were asking. |
Quote:
Code:
if maxLen < len(bar): maxLen = len(bar) |
Quote:
... and yes, max(a,b) that's not what I'm asking - just trying to put up a generic example. Of course, I would exploit the functionality of such an operation if it existed. Thanks for the replies. |
All times are GMT -5. The time now is 02:56 PM. |