First post so bear with me if I'm being a numpty ...
Is it me or is there something slightly counter intuitive and thus not
so pythonesque about this:
>>> s = ''
>>> if s: True
... else: False
...
False
>>> s and eval(s)
''
>>>
Regards,
AJ
Hi,
yes, the following evaluates to False:
empty String: ''
empty list: []
empty tuple: ()
empty dict: {}
0, None
and False of course
Regards,
Ralf
Thanks for that but why:
>>> '' and True
''
Surely that should be False?!?
Regards,
AJ
Hi,
> Hi Ralf,
>
> Thanks for that but why:
>
> >>> '' and True
> ''
>
> Surely that should be False?!?
Python does "short-circuit evaluation" [1]
"and" and "or" return one of its operands as soon as the outcome is
determined, not just True or False.
'' is a false value, as false as False itself :)
After seeing that, there is no point in evaluating the second operand
(True) because the final result cannot be true; so Python just returns the
first operand.
[1] http://en.wikipedia.org/wiki/Short_circuit_evaluation
--
Gabriel Genellina
"The expression x and y first evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting value is
returned."
hth,
vbr
> Thanks for that but why:
>>>> '' and True
> ''
> Surely that should be False?!?
It is:
#v+
>>> bool('' and True)
False
#v-
Bernd
--
No time toulouse
> yes, the following evaluates to False:
A much better way of describing the effect would be to say that the
following are treated as false (no capital letter!) in a conditional
context.
> empty String: ''
> empty list: []
> empty tuple: ()
> empty dict: {}
*any* empty container ...
> 0, None
> and False of course
and objects which adhere to the protocol
I'll shut up now. ;)
AJ
On 31 Jan 2009, at 12:27, Vlastimil Brom wrote:
> 2009/1/31 AJ Ostergaard <a...@cubbyhole.net>:
>> Hi Ralf,
>>
>> Thanks for that but why:
>>
>>>>> '' and True
>> ''
>>
>> Surely that should be False?!?
>>
No, deliberately not. Read this for Python 3.0
http://docs.python.org/3.0/reference/expressions.html#boolean-operations
and/or this for Python 2.X
http://docs.python.org/reference/expressions.html#boolean-operations
[essentially the only difference in 3.0 is a change to the protocol
that allows a non-builtin object to say whether it is true or false]
> yes, the following evaluates to False:
> empty String: ''
> empty list: []
> empty tuple: ()
> empty dict: {}
> 0, None
> and False of course
More precisely: All the above evaluate as Boolean false. But only one
of them evaluates to False: the object bound to the name ‘False’.
>>> bool(None) == bool(False)
True
>>> None == False
False
>>> None is False
False
>>> bool('') == bool(False)
True
>>> '' == False
False
>>> '' is False
False
>>> bool(0) == bool(False)
True
>>> 0 == False
False
>>> 0 is False
False
--
\ “What is it that makes a complete stranger dive into an icy |
`\ river to save a solid gold baby? Maybe we'll never know.” —Jack |
_o__) Handey |
Ben Finney
Why? The first value evaluates to False in a boolean context and
thus is returned in the above statement due to short circuit
evaluation but is not itself False. You wouldn't expect the following
statement to be True.
>>> '' is False
False
--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
The fact is that any left-hand operand that evaluates to false in a
Boolean context can be used as it stands rather than being converted to
Boolean first. So the conversion is essentially useless processing.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
But that doesn't mean "x is False" in the strict Python
expression sense of the phrase. It means if bool(x) is False
(or something reasonably close to that).
> its value is returned; otherwise, y is evaluated and the
> resulting value is returned."
--
Grant
No. Please read the section in the language reference about the and/or
operators.
"and" will return the first false value, or the right side. Thus
'' and True -> ''
True and '' -> ''
'a' and True -> True
True and 'a' -> 'a'
"or" does the same, obviously with the or-semantics:
'' or False -> False
False or '' -> ''
'' or True -> True
True or '' -> True
'a' or False -> 'a'
False or 'a' -> 'a'
Diez
I'm not suggesting it's not operating as advertised - I'm suggesting the 'advertising' is slightly sguiffy if you catch my drift. I guess it's just me that finds it slightly counter intuitive. Surely intuitively the expression is "and" and therefore should always return a boolean?
For example:
print name_from_form or default_name
or:
main(sys.arg[1:] or ['default', 'args'])
--Scott David Daniels
Scott....@Acm.Org