Empty string is False right?

A

AJ Ostergaard

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:
.... else: False
....
False
Regards,
AJ
 
R

Ralf Schoenian

AJ said:
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:

.... else: False
....
False

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
 
R

Ralf Schoenian

AJ said:
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:

.... else: False
....
False

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
 
A

AJ Ostergaard

Hi Ralf,

Thanks for that but why:
''

Surely that should be False?!?

Regards,
AJ


AJ said:
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:
.... else: False
....
False
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
 
G

Gabriel Genellina

Hi Ralf,

Thanks for that but why:

''

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
 
J

John Machin

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
 
A

AJ Ostergaard

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
 
D

D'Arcy J.M. Cain

''

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.
False
 
S

Steve Holden

AJ said:
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
 
D

Diez B. Roggisch

AJ said:
Hi Ralf,

Thanks for that but why:

''

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
 

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

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,284
Latest member
alphabetsalphabets

Latest Threads

Top