Eval of expr with 'or' and 'and' within

N

Nick the Gr33k

I started another thread because the last one was !@#$'ed up by
irrelevant replies and was difficult to jeep track.
abcd

Can understand that, it takes the first string out of the 3 strings that
has a truthy value.
True

No clue. since the expression in parenthesis returns 'abcd' how can 'k'
contained within 'abcd' ?
ijkl

Seems here is returning the last string out of 3 strings, but have no
clue why Python doing this.

yes, since expression returns 'ijkl', then the in operator can detect
the 'k' character within the returned string.

This is all iw ant to know.
 
R

Robert Kern

I started another thread because the last one was !@#$'ed up by irrelevant
replies and was difficult to jeep track.

abcd

Can understand that, it takes the first string out of the 3 strings that has a
truthy value.

True

No clue. since the expression in parenthesis returns 'abcd' how can 'k'
contained within 'abcd' ?

ijkl

Seems here is returning the last string out of 3 strings, but have no clue why
Python doing this.


yes, since expression returns 'ijkl', then the in operator can detect the 'k'
character within the returned string.

This is all iw ant to know.

This is all you need to read:

http://docs.python.org/2/reference/expressions.html#boolean-operations

Note the difference between how "or" and "and" each short-circuit. That is why
the (name or month or year) returns the first truthy value while (name and month
and year) returns the last truthy value. When "or" finds the first truthy value,
it can stop looking since the whole expression must be truthy no matter what the
values are after it. "and" cannot stop looking until it finds a falsy value or
runs out of values to look at.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

Grant Edwards

I started another thread

no kidding.
because the last one was !@#$'ed up by irrelevant replies and was
difficult to jeep track.

abcd

Can understand that, it takes the first string out of the 3 strings
that has a truthy value.

Yes, it does. That's the way the language is defined to work. If you
don't like it, pick a different language.
True

No clue. since the expression in parenthesis returns 'abcd'

No it doesn't. Try it:

Python 2.7.3 (default, Mar 20 2013, 14:16:24)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
how can 'k' contained within 'abcd' ?

It doesn't
 
N

Nick the Gr33k

no kidding.


Yes, it does. That's the way the language is defined to work. If you
don't like it, pick a different language.

I can understand OR stops at the first value when it finds it truthy,
and doesn't care for the other expr parameters.

And that logical since we say:

(name or month or year)
is like saying:

print whatever of those 3 are True, at least 1 of them, if it doesn't
not find any though for some reason it defaults to the last variable of
the expression(another mystery for me)

the way i realize it is just that for this boolean evaluation of the
expression to result in True if any of the above vars holds a truthy value.


Nice try, moving me to another newsgroup list :)
True

No clue. since the expression in parenthesis returns 'abcd'

No it doesn't. Try it:

Python 2.7.3 (default, Mar 20 2013, 14:16:24)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
how can 'k' contained within 'abcd' ?
Um sorry was a typo.

I mean the expr eval results in the value of var year which is 'ijkl'.

So yes it does.

My question is why the expr (name and month and year) result in the
value of the last variable whic is variable year?

I cannot understand AND.

Isn't it like saying that:
(name and month and year)

all of these variables have to have truthy values so for the boolaean
eval of the expr result in True?
 
S

Steven D'Aprano

On 2013-06-14 10:50, Nick the Gr33k wrote: [snip question]
This is all iw ant to know.

This is all you need to read:

http://docs.python.org/2/reference/expressions.html#boolean-
operations


Thank you Robert for contributing a helpful response.

To everyone else... I know that Nikos' posts are draining. Sometimes he
brings me to the brink of despair too. But if you aren't part of the
solution, you are part of the problem: writing short-tempered, insulting
posts after short-tempered, insulting post doesn't teach him, it just
adds to EVERYBODY'S frustration with this never-ending serious of threads.

Please keep the snarky comments offlist. Contribute something helpful if
you like, mute the thread or killfile Nikos if you must, but adding to
the volume of unproductive junk doesn't do anyone any favours.

Thank you.
 
R

rusi

On 2013-06-14 10:50, Nick the Gr33k wrote: [snip question]
This is all iw ant to know.
This is all you need to read:

operations

Thank you Robert for contributing a helpful response.

To everyone else... I know that Nikos' posts are draining. Sometimes he
brings me to the brink of despair too. But if you aren't part of the
solution, you are part of the problem: writing short-tempered, insulting
posts after short-tempered, insulting post doesn't teach him, it just
adds to EVERYBODY'S frustration with this never-ending serious of threads..

Notice your spelling of series Steven?

From which I suggest you seriously(!) consider my earlier suggestion
that Nikos is an infectious disease.
 
N

Nobody

My question is why the expr (name and month and year) result in the
value of the last variable whic is variable year?

For much the same reason that an OR expression returns the first true
value.

"or" and "and" only evaluate as many arguments are required in order to
determine the correct result (aka "short-circuit evaluation"). If the
first argument of "or" is true, or the first argument of "and" is false,
the second argument isn't evaluated (this is important if evaluation can
have side effects).

The operators can be expressed as:

True or X = True
False or X = X

False and X = False
True and X = X

Note that in the short-circuit case, the result has the same sense (true
or false) as the first argument, while in the other case the result has
the same sense as the second argument.

Python implements these operators by returning the actual value which
determined the result of the expression rather than simply True or False.

If the result is known after evaluating the first argument, the first
argument is returned. If it has to evaluate the second argument, the
second argument is returned (by that point it has already forgotten
the value of the first argument).
 
B

Benjamin Kaplan

Interesting. I'd have thought a boolean expression would return True or
False, not a string. Learn something new every day.

Python didn't have a Boolean type for quite some time (2.2?). Until then,
True and False were ints. Since every object had a Boolean value, you
didn't need to turn the result into an integer. In an "and" clause, python
returns the first false value or the last value, because that will evaluate
to the correct Boolean value. In an "or" clause, python returns the first
true value or the last value. When Python finally got a Boolean type, no
one wanted to break backwards compatibility for this.
 
S

Steven D'Aprano

Interesting. I'd have thought a boolean expression would return True or
False, not a string. Learn something new every day.

Correct. In Python, all boolean expressions are duck-typed: they aren't
restricted to True and False, but to any "true-ish" and "false-ish"
value, or as the Javascript people call them, truthy and falsey values.

Unlike Javascript though, Python's idea of truthy and falsey is actually
quite consistent:


Truthy:
- True
- any non-zero number
- any non-empty string
- any non-empty list/tuple/dict/set etc.
- object()
- anything else best considered as "something"


Falsey:
- False
- zero
- empty string
- empty list/tuple/dict/set etc.
- None
- anything else best considered as "nothing"


There are a couple of anomalies -- the timestamp representing midnight is
falsey, because it is implemented as a zero number of seconds; also
exhausted iterators and generators ought to be considered falsey, since
they are empty, but because they don't know they are empty until called,
they are actually treated as truthy. But otherwise, the model is very
clean.
 
N

Nick the Gr33k

On 14/6/2013 7:47 μμ, Benjamin Kaplan wrote:
In an "and" clause,
python returns the first false value or the last value, because that
will evaluate to the correct Boolean value. In an "or" clause, python
returns the first true value or the last value. When Python finally got
a Boolean type, no one wanted to break backwards compatibility for this.


This is exactly what i dont understand and thats why i keep asking and
people call me an idiot. I just dont understand why it behaves like that.

Why return first or last value?

because that will evaluate to the correct Boolean value ????

How do you mean? Please elaborate.
 
R

Robert Kern

On 14/6/2013 7:47 μμ, Benjamin Kaplan wrote:
In an "and" clause,


This is exactly what i dont understand and thats why i keep asking and people
call me an idiot. I just dont understand why it behaves like that.

Why return first or last value?

because that will evaluate to the correct Boolean value ????

How do you mean? Please elaborate.

Please read the link I gave. It explains why.

http://docs.python.org/2/reference/expressions.html#boolean-operations

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
M

Michael Torrie

Correct. In Python, all boolean expressions are duck-typed: they aren't
restricted to True and False, but to any "true-ish" and "false-ish"
value, or as the Javascript people call them, truthy and falsey values.
<snip>
There are a couple of anomalies -- the timestamp representing midnight is
falsey, because it is implemented as a zero number of seconds; also
exhausted iterators and generators ought to be considered falsey, since
they are empty, but because they don't know they are empty until called,
they are actually treated as truthy. But otherwise, the model is very
clean.

Good explanation! Definitely enlightened me. Thank you.
 
M

MRAB

Good explanation! Definitely enlightened me. Thank you.
The general rule is that an object is true-ish unless it's false-ish
(there are fewer false-ish objects than true-ish objects, e.g. zero vs
non-zero int).
 
C

Chris Angelico

The general rule is that an object is true-ish unless it's false-ish
(there are fewer false-ish objects than true-ish objects, e.g. zero vs
non-zero int).

With a few random oddities:
True

I somehow expected NaN to be false. Maybe that's just my expectations
that are wrong, though.

ChrisA
 
A

Antoon Pardon

Op 14-06-13 18:09, Steven D'Aprano schreef:
On 2013-06-14 10:50, Nick the Gr33k wrote: [snip question]
This is all iw ant to know.

This is all you need to read:

http://docs.python.org/2/reference/expressions.html#boolean-
operations


Thank you Robert for contributing a helpful response.

To everyone else... I know that Nikos' posts are draining. Sometimes he
brings me to the brink of despair too. But if you aren't part of the
solution, you are part of the problem: writing short-tempered, insulting
posts after short-tempered, insulting post doesn't teach him, it just
adds to EVERYBODY'S frustration with this never-ending serious of threads.

That depends on your view. Maybe there are people who view you as part
of the problem because you keep giving Nikos hope other people will
solve his problems without him having to do anything significant himself.
Please keep the snarky comments offlist. Contribute something helpful if
you like, mute the thread or killfile Nikos if you must, but adding to
the volume of unproductive junk doesn't do anyone any favour.

In my opinion the only really helpful thing would be to ignore nikos. He
has been given plenty or links, to read through in order to better his
understanding and i think it is about time people made it clear they
would no longer spoon feed him. Since that is probably not going to
happen and you will probably continue contributing to the annoyance
of other list members by continuing "helping" nikos, I don'y see why
other can't continue to the annoyance in a away that they themselves may
find somewhat amusing.
 
R

rusi

Op 14-06-13 18:09, Steven D'Aprano schreef:








On 2013-06-14 10:50, Nick the Gr33k wrote: [snip question]
This is all iw ant to know.
This is all you need to read:
   http://docs.python.org/2/reference/expressions.html#boolean- operations

Thank you Robert for contributing a helpful response.
To everyone else... I know that Nikos' posts are draining. Sometimes he
brings me to the brink of despair too. But if you aren't part of the
solution, you are part of the problem: writing short-tempered, insulting
posts after short-tempered, insulting post doesn't teach him, it just
adds to EVERYBODY'S frustration with this never-ending serious of threads.

That depends on your view. Maybe there are people who view you as part
of the problem because you keep giving Nikos hope other people will
solve his problems without him having to do anything significant himself.
Please keep the snarky comments offlist. Contribute something helpful if
you like, mute the thread or killfile Nikos if you must, but adding to
the volume of unproductive junk doesn't do anyone any favour.

In my opinion the only really helpful thing would be to ignore nikos. He
has been given plenty or links, to read through in order to better his
understanding and i think it is about time people made it clear they
would no longer spoon feed him. Since that is probably not going to
happen and you will probably continue contributing to the annoyance
of other list members by continuing "helping" nikos, I don'y see why
other can't continue to the annoyance in a away that they themselves may
find somewhat amusing.

Thanks Antoon.
You are echoing the sentiments of probably hundreds of list
subscribers.

That said I would like to add a few points.
1. Beginning teachers (and young parents) are taught that one never
should say: "You are a bad boy!" but rather "THIS behavior wont do"
That nikos behavior is unacceptable has been reiterated by dozens of
members in 100s of posts. That does not mean Nikos the person is
unacceptable. We should always give him the room to change if/when he
chooses to.

2. The recent responses from Robert Kern are in my view the ideal. In
summary it runs thus:
Stupid question no. 6457 from Nikos: ...
Robert : Look this up <link>
Nikos: I dont understand
Robert: <Link> explains
Nikos: I DONTU NDERSTND
Robert: <Link> explains (repeated as often as needed)

When (if) finally Nikos actually reads the link and asks a more
specific/intelligent question, <link> can be replaced by more specific
<link'>
 
J

Joshua Landau

2. The recent responses from Robert Kern are in my view the ideal. In
summary it runs thus:
Stupid question no. 6457 from Nikos: ...
Robert : Look this up <link>
Nikos: I dont understand
Robert: <Link> explains
Nikos: I DONTU NDERSTND
Robert: <Link> explains (repeated as often as needed)

When (if) finally Nikos actually reads the link and asks a more
specific/intelligent question, <link> can be replaced by more specific
<link'>

+1

Please, everyone else, just do this. This will calm the beast, I
assure you, at least somewhat. If you're worried about not being
"helpful", don't - this is as much help as ever. It's just it takes
less time, and a *lot* less space.
 
J

Jussi Piitulainen

Nick said:
Why return first or last value?

because that will evaluate to the correct Boolean value ????

That value will either behave exactly the same as the Boolean value
you call correct, or else it will be more useful. That is, most of the
time it doesn't matter, and when it matters, Python's way is better.

You can turn any expression E into a strictly Boolean value by writing
bool(E) instead. Often E is already guaranteed to be a Boolean and the
whole question does not arise in the first place.

This doesn't prevent you from writing your program.
 
G

Grant Edwards

For much the same reason that an OR expression returns the first true
value.

"or" and "and" only evaluate as many arguments are required in order to
determine the correct result (aka "short-circuit evaluation"). If the
first argument of "or" is true, or the first argument of "and" is false,
the second argument isn't evaluated (this is important if evaluation can
have side effects).

There are two completely orthogonal concepts here:

1. Short-circuit evaluation. Many languages do this. AFAICT, this
isn't what he's asking about, but this is what people keep
explaining.

2. Returning one the objects that result from the evaluation of the
operands instead of returning True or False.

This is what seems to be confusing him. This is much less common
than short-circuit evaluation. C does short-circuit evaluation of
&& and || operators, but the result is always 1 or 0 (true of
false). Instead of always returning True or False (which could be
done and still preserver short-circuit evaluation), Python returns
one of the operands or False.

If you also have 1. there are cases where the value returned by
the "or" operator is useful apart from it's "truthyness" value.
There may be cases where the result returned by the "and" operator
is useful apart from it's truthyness, but that seems to be less
common. Taking advantage of that fact can lead to some
hard-to-read code, so it's often discouraged as being too clever.

It's important to note that these are somewhat orthogonal:

You can have #1 and #2 (like Python).

You can have #1 without #2 (like C).

You can have #2 without #1

You can have neither #1 or #2

But again, #2 is more useful if you also have #1.
 

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
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top