Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Empty string is False right?

4 views
Skip to first unread message

AJ Ostergaard

unread,
Jan 31, 2009, 6:35:05 AM1/31/09
to pytho...@python.org
Hello,

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

Ralf Schoenian

unread,
Jan 31, 2009, 7:12:41 AM1/31/09
to AJ Ostergaard, pytho...@python.org
AJ Ostergaard wrote:
> Hello,
>
> 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

AJ Ostergaard

unread,
Jan 31, 2009, 7:16:19 AM1/31/09
to pytho...@python.org
Hi Ralf,

Thanks for that but why:

>>> '' and True
''

Surely that should be False?!?

Regards,
AJ

Ralf Schoenian

unread,
Jan 31, 2009, 7:12:41 AM1/31/09
to AJ Ostergaard, pytho...@python.org
AJ Ostergaard wrote:
> Hello,
>
> 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,

Gabriel Genellina

unread,
Jan 31, 2009, 7:26:50 AM1/31/09
to pytho...@python.org
En Sat, 31 Jan 2009 10:16:19 -0200, AJ Ostergaard <a...@cubbyhole.net>
escribió:

> 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

Vlastimil Brom

unread,
Jan 31, 2009, 7:27:14 AM1/31/09
to AJ Ostergaard, pytho...@python.org
2009/1/31 AJ Ostergaard <a...@cubbyhole.net>:

> Hi Ralf,
>
> Thanks for that but why:
>
>>>> '' and True
> ''
>
> Surely that should be False?!?
>
> Regards,
> AJ
>
>
see the docs:
http://docs.python.org/reference/expressions.html#boolean-operations

"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

Bernd Nawothnig

unread,
Jan 31, 2009, 7:27:10 AM1/31/09
to
On 2009-01-31, AJ Ostergaard wrote:

> Thanks for that but why:

>>>> '' and True
> ''

> Surely that should be False?!?

It is:

#v+
>>> bool('' and True)
False
#v-

Bernd

--
No time toulouse

John Machin

unread,
Jan 31, 2009, 7:32:16 AM1/31/09
to
On Jan 31, 11:12 pm, Ralf Schoenian <r...@schoenian-online.de> wrote:

> 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

AJ Ostergaard

unread,
Jan 31, 2009, 7:36:03 AM1/31/09
to pytho...@python.org
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?

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?!?
>>

John Machin

unread,
Jan 31, 2009, 7:43:25 AM1/31/09
to
On Jan 31, 11:16 pm, AJ Ostergaard <a...@cubbyhole.net> wrote:
> 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]

Ben Finney

unread,
Jan 31, 2009, 7:56:43 AM1/31/09
to
Ralf Schoenian <ra...@schoenian-online.de> writes:

> 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

D'Arcy J.M. Cain

unread,
Jan 31, 2009, 8:24:06 AM1/31/09
to AJ Ostergaard, pytho...@python.org
On Sat, 31 Jan 2009 12:16:19 +0000
AJ Ostergaard <a...@cubbyhole.net> wrote:
> >>> '' and True
> ''
>
> Surely that should be False?!?

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.

Steve Holden

unread,
Jan 31, 2009, 8:27:20 AM1/31/09
to pytho...@python.org
AJ Ostergaard wrote:
> 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?
>
> I'll shut up now. ;)
>
You might think so, and it wouldn't be an entirely unreasonable thought,
but in practice it makes a lot of sense to retain the original value
where possible.

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/

Grant Edwards

unread,
Jan 31, 2009, 9:15:58 AM1/31/09
to
On 2009-01-31, Vlastimil Brom <vlastim...@gmail.com> wrote:
> 2009/1/31 AJ Ostergaard <a...@cubbyhole.net>:
>> Hi Ralf,
>>
>> Thanks for that but why:
>>
>>>>> '' and True
>> ''
>>
>> Surely that should be False?!?
>>
>> Regards,
>> AJ
>>
>>
> see the docs:
> http://docs.python.org/reference/expressions.html#boolean-operations
>
> "The expression x and y first evaluates x; if x is false,

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

Diez B. Roggisch

unread,
Jan 31, 2009, 11:09:37 AM1/31/09
to
AJ Ostergaard schrieb:

> Hi Ralf,
>
> Thanks for that but why:
>
> >>> '' and True
> ''
>
> Surely that should be False?!?

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

Stephen Hansen

unread,
Jan 31, 2009, 12:01:37 PM1/31/09
to pytho...@python.org
On Sat, Jan 31, 2009 at 4:36 AM, AJ Ostergaard <a...@cubbyhole.net> wrote:
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?

Boolean operators just aren't guaranteed to return a boolean /type/ -- but instead a boolean /expression/, that if evaluated is true or false. The difference is important -- and although it might be partly for historical reasons (after all, there was no boolean /type/ until Python 2.3. That's not really all that long ago), it also has quite a few useful properties that aren't necessecarily immediately obvious but are used in a variety of places.

A common one is the and/or "ternary" expression that pre-dates the addition of conditional expressions to Python 2.5:

>>> test = True
>>> test and "Apples" or "Oranges"
'Apples'
>>> test = False
>>> test and "Apples" or "Oranges"
'Oranges'

In Python 2.5., that can be rewritten to:

>>> test = True
>>> "Apples" if test else "Oranges"
'Apples'
>>> test = False
>>> "Apples" if test else "Oranges"
'Oranges'

Its part of why you aren't supposed to compare boolean results/tests/expressions against True or False directly usually (besides /just/ style). You do:

if expression:
   print "Was true!"

Not:

if expression is True:
    print "Was true!"

--Stephen

signature.asc

Scott David Daniels

unread,
Jan 31, 2009, 12:48:24 PM1/31/09
to
Steve Holden wrote:
> AJ Ostergaard wrote:
>> 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?
...

> You might think so, and it wouldn't be an entirely unreasonable thought,
> but in practice it makes a lot of sense to retain the original value
> where possible.

For example:
print name_from_form or default_name
or:
main(sys.arg[1:] or ['default', 'args'])

--Scott David Daniels
Scott....@Acm.Org

0 new messages