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

2013-06-15 Thread Nick the Gr33k

On 15/6/2013 12:48 μμ, Lele Gaifax wrote:

but those 2 gives the same results back

"k" in (name+month+year) == "k" in (name and month and year)
True

so both seem to work as expected.

That happens only by chance: it seems you now understand the evaluation
of "boolean" expressions in Python, so the following should be clear to
you:


yes indeed!

if we had questioned python for: "k" in (name and year and month)

that would have returned the argument month back which is "efgh" and 
then the if would have evaled to false since 'k' isn't part of the latter.


"k" in (name and month and year) != "k" in (name and year and month)

As wee see, the order of the arguments in an expression matters.

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-15 Thread Nick the Gr33k

On 15/6/2013 12:48 μμ, Lele Gaifax wrote:

Nick the Gr33k  writes:



but those 2 gives the same results back

"k" in (name+month+year) == "k" in (name and month and year)
True

so both seem to work as expected.


That happens only by chance: it seems you now understand the evaluation
of "boolean" expressions in Python, so the following should be clear to
you:


"k" in ("there" + "is" + "a" + "k" + "character" + "somewhere")

True


ALL strings in the parenthesis are being concatenated to a BIG string, 
like ('k' in BIG_string) which returns the Boolean value of True since 
'k' is contained within it.



"k" in ("there" and "is" and "a" and "k" and "character" and "somewhere")

False


first the expression of the parenthesis evaluation.

The argument being returned from ("there" and "is" and "a" and "k" and 
"character" and "somewhere") expresssion, is the one that is responsible 
for the determination of the expression's evaluation.

That would be the last argument, string "somewhere" because:

1. All previous strings before the last one were found truthies, so it 
was turn of the last one to be evaled too.


2. Since "somewhere" is a truthy of course for being a non-empty string 
then ALL arguments of the expression are found TRUE which results for 
the expression to stand TRUE.


3. What determined the expression to stand TRUE was the evaluation of 
the last argument "somewhere", so the latter was the key factor for that 
to happen, hence its being returned back as a result(thats how Python works)


4. so it's like checking if ('k' in "somewhere") which return the 
Boolean value of False since the aforementioned char isn't contained in 
the aforementioned string.


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-15 Thread Lele Gaifax
Nick the Gr33k  writes:

> On 15/6/2013 3:14 πμ, Cameron Simpson wrote:
>> But for what you are doing, "and" and "or" are not good operations.
>>
>> Something like:
>>
>>"k" in (name+month+year)
>>
>> or
>>
>>"k" in name or "k" in month or "k" in year
>
> Used to wrote it myself like the latter but needed a more compact way
> of writing it for clarity so i used the former.
>
> but those 2 gives the same results back
>
> "k" in (name+month+year) == "k" in (name and month and year)
> True
>
> so both seem to work as expected.

That happens only by chance: it seems you now understand the evaluation
of "boolean" expressions in Python, so the following should be clear to
you: 

>>> "k" in ("there" + "is" + "a" + "k" + "character" + "somewhere")
True
>>> "k" in ("there" and "is" and "a" and "k" and "character" and "somewhere")
False

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-15 Thread Nick the Gr33k

On 15/6/2013 3:14 πμ, Cameron Simpson wrote:

On 14Jun2013 12:50, Nikos as SuperHost Support  wrote:
| I started another thread because the last one was !@#$'ed up by
| irrelevant replies and was difficult to jeep track.
|
| >>> name="abcd"
| >>> month="efgh"
| >>> year="ijkl"
|
| >>> print(name or month or year)
| abcd
|
| Can understand that, it takes the first string out of the 3 strings
| that has a truthy value.
|
| >>> print("k" in (name and month and year))
| True
|
| No clue. since the expression in parenthesis returns 'abcd' how can
| 'k' contained within 'abcd' ?

Did you print the result of "name and month and year"? It is the
_last_ value (if true at all).  You used "or" in the first example
and "and" in the second.


okey, lets see it again:

>>> print (name or month or year)
abcd
>>>

Yes, 'k' isn't contained in the result string 'abcd'

>>> print (name and month and year)
hijk
>>> print( "k" in (name and month and year) )
True

Yes they work as expected, i was mistaken, sorry.




| >>> print(name and month and year)
| ijkl
|
| Seems here is returning the last string out of 3 strings, but have
| no clue why Python doing this.

To evaluate an "and" it must test all of them to be true, and it
keeps the last value tested.  (Or False, of course, if they are not
all true, in which case Python stops testing at the first False).


Yes, i know it behaves like that, the question is why:

As "Nobody" explained to me, the reason is that Python expressions 
results back the argument that determined the evaluation of the Boolean 
expression, which in turn can be a truthy or a falsey used in 'or' or 
'and' respectively.


Returning a truthy value equals True
returning a falsey value equals False

so it all boils down to the Booleans type of values True or False.



But for what you are doing, "and" and "or" are not good operations.

Something like:

   "k" in (name+month+year)

or

   "k" in name or "k" in month or "k" in year


Used to wrote it myself like the latter but needed a more compact way of 
writing it for clarity so i used the former.


but those 2 gives the same results back

"k" in (name+month+year) == "k" in (name and month and year)
True

so both seem to work as expected.
--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-15 Thread Nick the Gr33k

On 14/6/2013 7:42 μμ, Nobody wrote:

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


which in turn the actual value being returned is a truthy or a falsey.

That cleared the mystery in my head entirely.
I wouldn't have asked so many follow-up questions in the thread if i 
received that kind of a response.


Thank you very much for this response.


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).


So, the less it has to calculate to determine the correct result of an 
expression the better.


Thanks again very much.

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-15 Thread Nick the Gr33k

On 15/6/2013 10:49 πμ, Denis McMahon wrote:

On Sat, 15 Jun 2013 10:04:41 +0300, Nick the Gr33k wrote:


I called my self 'Ferrous Cranus'(this is what a guy from a forum
initially called me for being hard-headed :-) ) because i'm indeed
hardheaded and if i believe that 1 thing should have worked in some way
i cant change my mind easily that it works in another fashon(only if the
counter-arguments are strong).


Then you need to stop trying to write python code, because you refuse to
accept how python works, and python is not going to change for you!


Given the right counter-arguments one can't make a stand against 
something that is without no doubt True.



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-15 Thread Denis McMahon
On Sat, 15 Jun 2013 10:04:41 +0300, Nick the Gr33k wrote:

> I called my self 'Ferrous Cranus'(this is what a guy from a forum
> initially called me for being hard-headed :-) ) because i'm indeed
> hardheaded and if i believe that 1 thing should have worked in some way
> i cant change my mind easily that it works in another fashon(only if the
> counter-arguments are strong).

Then you need to stop trying to write python code, because you refuse to 
accept how python works, and python is not going to change for you!

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-15 Thread Nick the Gr33k

On 15/6/2013 9:50 πμ, alex23 wrote:

Please keep the snarky comments offlist.

Tried that. He posts them back here.

Alternatively, I'd ask that if you're so willing to deal with him, that the 
*two of you* take this show offlist instead? I'm genuinely curious as to 
whether he'd agree to this: given his propensity for changing his mail headers 
so regularly, the whole thing still screams performance troll. (I mean, 
seriously, how many days elapsed between his being burned by Chris Angelo and 
him offering to give you access to his server? Isn't refusing to budge from a 
position the defining characteristic of a ferrous cranus?)


I called my self 'Ferrous Cranus'(this is what a guy from a forum 
initially called me for being hard-headed :-) ) because i'm indeed 
hardheaded and if i believe that 1 thing should have worked in some way 
i cant change my mind easily that it works in another fashon(only if the 
counter-arguments are strong).

Being strong-headed != acting as a troll


About the changing of NNTP hosts. (well Google Groups gave me and you 
issues with its constant adding of '\n' between lines. I was given many 
complains about that so as Chris suggested i ditched Google Groups and 
try to find out how i can use TB instead.


As for the mail, i decided since my questions concern python and my 
website to change the gmail address to support@ which is more relevant.


I have also correct each time my spelling before i post.

Now as for you, you should be thankful for Steven and Cameron answers to 
this groups because answering my questions.


1. help me
2. help others, which afraid to ask, in the propensity of being laughed upon
3. help other groups members like Michale Torrie whose admitted that 
some of the explanation was useful to him and he didn't knew that 
Boolean expressions can return string values instead of True or False.
3. help you, because you are being educated yourself too, because i 
sincerely doubt if you knew anything i i have asked.


So, stop complaining and try to be helpful here or just  mute my thread 
as Chris suggested as a working solution opposed to kill filing.



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread alex23
On Saturday, 15 June 2013 02:09:20 UTC+10, Steven D'Aprano  wrote:
> 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.

What about our frustration that this thread has become so overwhelmingly his 
support group? It's not like there isn't a list where he could post absolute 
beginner questions:

http://mail.python.org/mailman/listinfo/tutor

Although I'm pretty sure their first response would be "read the docs / play 
with the interpreter", neither of which he's shown any desire to do.

> Please keep the snarky comments offlist. 

Tried that. He posts them back here.

Alternatively, I'd ask that if you're so willing to deal with him, that the 
*two of you* take this show offlist instead? I'm genuinely curious as to 
whether he'd agree to this: given his propensity for changing his mail headers 
so regularly, the whole thing still screams performance troll. (I mean, 
seriously, how many days elapsed between his being burned by Chris Angelo and 
him offering to give you access to his server? Isn't refusing to budge from a 
position the defining characteristic of a ferrous cranus?)
-- 
http://mail.python.org/mailman/listinfo/python-list


NANs [was Re: Eval of expr with 'or' and 'and' within]

2013-06-14 Thread Steven D'Aprano
On Sat, 15 Jun 2013 12:03:08 +1000, Cameron Simpson wrote:

> | ... even taking that into account! *wink* |
> | Everyone is aware that there is more than one NAN, right?
> 
> I was not. Interesting.
> 
> | If my
> | calculations are correct, there are 9007199254740992 distinct float
> | NANs in Python (although there is no direct way of distinguishing
> | them).
> 
> Wouldn't id() do it? At least in terms of telling them apart? I gather
> they're not inspectable in Python?

I'm not talking about different *objects*. It would be terribly wasteful 
for Python to pre-allocate 9-gazillion NAN objects. I'm talking about 
distinct values, in the C-double sense.

Python may or may not cache floats in general. In general, it doesn't:

py> x = 2.5
py> y = 2.5
py> x is y
False


but in principle Python might choose to cache some, or all float objects, 
like it does with some ints and strings:

# simulated, not real
py> x = 0.0
py> y = 0.0
py> x is y
True

So you can always distinguish two floats, including NANs, *by value* with 
== and by *object identity* with id() and `is`. But that's not what I'm 
referring to.

The layout of a C double (the numeric value of a float object) is 64 bits:

|s|..e..|..f..|

where s is a single sign bit, e is an 11-bit biased exponent, and f is a 
52-bit binary fraction. If e is 2047 (0b111) then the double is a 
special value, either an INF or a NAN:

e = 2047, f == 0: double is +INF or -INF, depending on s

e = 2047, f != 0: double is a NAN

With a 52-bit f field, there are 4503599627370496 distinct payloads with 
the sign bit set, and another 4503599627370496 with it cleared. Python 
gives you no direct way of testing or setting that payload, or for that 
matter the sign bit, on a NAN, although you can use the struct module to 
cast a float to a 64-bit int and then do bit twiddling. But in principle, 
with 51 (why not 52? see below) bits available, you can stuff quite a 
fair bit of diagnostic information in a NAN, if you need to.

The high bit of f is reserved for a special purpose. If the high bit is 
set (i.e. f >> 51 == 1) the NAN is considered a signalling NAN, which (in 
implementations that comply with the IEEE 754 standard) are guaranteed to 
halt the calculation immediately. Those with it cleared are quiet NANs, 
and are intended to propagate through calculations[1], although that is 
configurable[2].




[1] It's not quite true that once a NAN has entered a calculation, it is 
guaranteed to be the final result. There are circumstances where NANs can 
legitimately disappear from a calculation, leaving an actual number.

[2] Python doesn't allow you to configure float's behaviour but the 
Decimal module does.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Chris Angelico
On Sat, Jun 15, 2013 at 12:03 PM, Cameron Simpson  wrote:
> On 15Jun2013 01:34, Steven D'Aprano  
> wrote:
> | Everyone is aware that there is more than one NAN, right?
>
> I was not. Interesting.
>
> | If my
> | calculations are correct, there are 9007199254740992 distinct float NANs
> | in Python (although there is no direct way of distinguishing them).
>
> Wouldn't id() do it? At least in terms of telling them apart?
> I gather they're not inspectable in Python?

You could recognize one float object as distinct from another, but
that's true of all floats:

>>> float("1.0") is float("1.0")
False

All NaNs are different in terms of the == operator, so conceptually
there are an infinite number of unique NaNs. The fact that they're
stored in memory using a certain number of bits means that there must
be a finite number of possible representations, but that's really an
implementation detail. I suppose you could figure out the
representation differences by fiddling with ctypes (in C I'd just use
a union), but that's really all.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Cameron Simpson
On 15Jun2013 01:34, Steven D'Aprano  
wrote:
| On Sat, 15 Jun 2013 00:09:31 +0100, Nobody wrote:
| 
| > On Sat, 15 Jun 2013 03:56:28 +1000, Chris Angelico wrote:
| >> With a few random oddities:
| > bool(float("nan"))
| >> True
| >> I somehow expected NaN to be false. Maybe that's just my expectations
| >> that are wrong, though.
| > 
| > In general, you should expect the behaviour of NaN to be the opposite of
| > what you expect.
| 
| ... even taking that into account! *wink*
| 
| Everyone is aware that there is more than one NAN, right?

I was not. Interesting.

| If my 
| calculations are correct, there are 9007199254740992 distinct float NANs 
| in Python (although there is no direct way of distinguishing them).

Wouldn't id() do it? At least in terms of telling them apart?
I gather they're not inspectable in Python?

Cheers,
-- 
Cameron Simpson 

2 strokes are quicker than 4.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Steven D'Aprano
On Sat, 15 Jun 2013 00:09:31 +0100, Nobody wrote:

> On Sat, 15 Jun 2013 03:56:28 +1000, Chris Angelico wrote:
> 
>> With a few random oddities:
>> 
> bool(float("nan"))
>> True
>> 
>> I somehow expected NaN to be false. Maybe that's just my expectations
>> that are wrong, though.
> 
> In general, you should expect the behaviour of NaN to be the opposite of
> what you expect.

... even taking that into account! *wink*


Everyone is aware that there is more than one NAN, right? If my 
calculations are correct, there are 9007199254740992 distinct float NANs 
in Python (although there is no direct way of distinguishing them). Half 
have the sign bit set, half do not; half are quiet NANs and half are 
signalling NANs. It would be too easy if "sign bit set" meant signalling, 
so in fact there are four equal-numbered groups of NANs, 2251799813685248 
each of:

+quiet
+signalling
-quiet
-signalling

where the - sign should be interpreted as "sign bit is set" rather than 
"negative", and + sign as "sign bit not set".

They differ according to their bit-pattern, or payload. Some systems 
actually give standard meanings to different bit patterns, e.g. in the 
old SANE (Standard Apple Numerics Environment) system, different NANs 
were produced according to different kinds of errors, e.g:

Payload  Description
===  ==
1Invalid sqrt
2Invalid addition, e.g. +INF + -INF
3Invalid division, e.g. 0/0
17   Convert invalid string
21   Attempt to create NAN with 0 as payload

etc.

(Assigning meaning to the payload is optional, according to the IEEE 754 
standard, if I recall correctly.)



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Cameron Simpson
On 14Jun2013 12:50, Nikos as SuperHost Support  wrote:
| I started another thread because the last one was !@#$'ed up by
| irrelevant replies and was difficult to jeep track.
| 
| >>> name="abcd"
| >>> month="efgh"
| >>> year="ijkl"
| 
| >>> print(name or month or year)
| abcd
| 
| Can understand that, it takes the first string out of the 3 strings
| that has a truthy value.
| 
| >>> print("k" in (name and month and year))
| True
| 
| No clue. since the expression in parenthesis returns 'abcd' how can
| 'k' contained within 'abcd' ?

Did you print the result of "name and month and year"? It is the
_last_ value (if true at all).  You used "or" in the first example
and "and" in the second.

| >>> print(name and month and year)
| ijkl
| 
| Seems here is returning the last string out of 3 strings, but have
| no clue why Python doing this.

To evaluate an "and" it must test all of them to be true, and it
keeps the last value tested.  (Or False, of course, if they are not
all true, in which case Python stops testing at the first False).

But for what you are doing, "and" and "or" are not good operations.

Something like:

  "k" in (name+month+year)

or

  "k" in name or "k" in month or "k" in year

is a more direct and accurate way to write what I imagine you're trying to do.

Cheers,
-- 
Cameron Simpson 

No one is completely worthless...  they can always serve as a bad example.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread MRAB

On 15/06/2013 00:06, Nobody wrote:

On Fri, 14 Jun 2013 16:49:11 +, Steven D'Aprano wrote:


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


Beyond that, if a user-defined type implements a __nonzero__() method then
it determines whether an instance is true or false. If it implements a
__len__() method, then an instance is true if it has a non-zero length.


It's __nonzero__ in Python 2, __bool__ in Python 3.
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Chris Angelico
On Sat, Jun 15, 2013 at 5:33 AM, Grant Edwards  wrote:
> On 2013-06-14, Chris Angelico  wrote:
>> On Sat, Jun 15, 2013 at 3:49 AM, MRAB  wrote:
>>> 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:
>>
> bool(float("nan"))
>> True
>>
>> I somehow expected NaN to be false. Maybe that's just my expectations
>> that are wrong, though.
>
> If you work with floating point long enough you realize that most of
> your expectations are wrong.  Sometimes.  Eventually.

NaN is like NULL in SQL. It's a weird beast. Just when you think
you've pinned it down, it slips out from under your taxonomic
classifications and thumbs its nose at you...

I think they're cousins to the platypus.

http://tvtropes.org/pmwiki/pmwiki.php/Main/EverythingsBetterWithPlatypi

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Nobody
On Sat, 15 Jun 2013 03:56:28 +1000, Chris Angelico wrote:

> With a few random oddities:
> 
 bool(float("nan"))
> True
> 
> I somehow expected NaN to be false. Maybe that's just my expectations
> that are wrong, though.

In general, you should expect the behaviour of NaN to be the opposite of
what you expect.

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Nobody
On Fri, 14 Jun 2013 16:49:11 +, Steven D'Aprano wrote:

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

Beyond that, if a user-defined type implements a __nonzero__() method then
it determines whether an instance is true or false. If it implements a
__len__() method, then an instance is true if it has a non-zero length.

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Nobody
On Fri, 14 Jun 2013 19:30:27 +, Grant Edwards wrote:

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

FWIW, Lisp also does this. But Lisp is slightly simpler as the only false
value is "nil", while everything else is true (including integer zero).

Although Python's any() and all() (which are closer to Lisp's "and" and
"or" insofar as they all work with any number of values, including zero)
always return True or False rather than the final value.

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Grant Edwards
On 2013-06-14, Nick the Gr33k  wrote:
> 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?

There are cases where it's useful not only to know wether an
expression was True or False, but _why_ it was true or false.

> because that will evaluate to the correct Boolean value 

Yes.  All values in Pyton have a "truthness" and you can be guaranteed
that 

 if A or B or C:

will behave exactly the same way whether the "or" operator returns
True or it returns one of it's operands.  

-- 
Grant Edwards   grant.b.edwardsYow! Where do your SOCKS
  at   go when you lose them in
  gmail.comth' WASHER?
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Grant Edwards
On 2013-06-14, Chris Angelico  wrote:
> On Sat, Jun 15, 2013 at 3:49 AM, MRAB  wrote:
>> 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:
>
 bool(float("nan"))
> True
>
> I somehow expected NaN to be false. Maybe that's just my expectations
> that are wrong, though.

If you work with floating point long enough you realize that most of
your expectations are wrong.  Sometimes.  Eventually.

-- 
Grant Edwards   grant.b.edwardsYow! My pants just went to
  at   high school in the Carlsbad
  gmail.comCaverns!!!
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Grant Edwards
On 2013-06-14, Nobody  wrote:
> On Fri, 14 Jun 2013 18:16:05 +0300, Nick the Gr33k wrote:
>
>> 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).

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. 
 
-- 
Grant Edwards   grant.b.edwardsYow! ... I have read the
  at   INSTRUCTIONS ...
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Jussi Piitulainen
Nick the Gr33k writes:

> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Joshua Landau
On 14 June 2013 19:37, rusi  wrote:
> 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 
> Nikos: I dont understand
> Robert:  explains
> Nikos: I DONTU NDERSTND
> Robert:  explains (repeated as often as needed)
>
> When (if) finally Nikos actually reads the link and asks a more
> specific/intelligent question,  can be replaced by more specific
> 

+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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread rusi
On Jun 14, 11:03 pm, Antoon Pardon 
wrote:
> Op 14-06-13 18:09, Steven D'Aprano schreef:
>
>
>
>
>
>
>
>
>
> > On Fri, 14 Jun 2013 11:14:16 +0100, Robert Kern wrote:
>
> >> 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.
>
> --
> Antoon Pardon.

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 
Nikos: I dont understand
Robert:  explains
Nikos: I DONTU NDERSTND
Robert:  explains (repeated as often as needed)

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

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Antoon Pardon

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

On Fri, 14 Jun 2013 11:14:16 +0100, Robert Kern wrote:


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.


--
Antoon Pardon.
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Chris Angelico
On Sat, Jun 15, 2013 at 3:49 AM, MRAB  wrote:
> 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:

>>> bool(float("nan"))
True

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

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread MRAB

On 14/06/2013 18:28, Michael Torrie wrote:

On 06/14/2013 10:49 AM, Steven D'Aprano wrote:

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.

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.


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).
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Michael Torrie
On 06/14/2013 10:49 AM, Steven D'Aprano wrote:
> 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.
> 
> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Robert Kern

On 2013-06-14 18:01, Nick the Gr33k wrote:

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.


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

--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread 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.

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Steven D'Aprano
On Fri, 14 Jun 2013 10:29:25 -0600, Michael Torrie wrote:

> On 06/14/2013 03:50 AM, Nick the Gr33k wrote:
>>  >>> print(name or month or year)
>> abcd
>>  >>> print(name and month and year)
>> ijkl
> 
> 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.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Benjamin Kaplan
On Jun 14, 2013 9:34 AM, "Michael Torrie"  wrote:
>
> On 06/14/2013 03:50 AM, Nick the Gr33k wrote:
> >  >>> print(name or month or year)
> > abcd
> >  >>> print(name and month and year)
> > ijkl
>
> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Nobody
On Fri, 14 Jun 2013 18:16:05 +0300, Nick the Gr33k wrote:

> 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).

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Michael Torrie
On 06/14/2013 03:50 AM, Nick the Gr33k wrote:
>  >>> print(name or month or year)
> abcd
>  >>> print(name and month and year)
> ijkl

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



-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread rusi
On Jun 14, 9:09 pm, Steven D'Aprano  wrote:
> On Fri, 14 Jun 2013 11:14:16 +0100, Robert Kern wrote:
> > 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.

Notice your spelling of series Steven?

>From which I suggest you seriously(!) consider my earlier suggestion
that Nikos is an infectious disease.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Steven D'Aprano
On Fri, 14 Jun 2013 11:14:16 +0100, Robert Kern wrote:

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


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Nick the Gr33k

On 14/6/2013 5:49 μμ, Grant Edwards wrote:

On 2013-06-14, Nick the Gr33k  wrote:


I started another thread


no kidding.


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


name="abcd"
month="efgh"
year="ijkl"



print(name or month or year)

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.


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 :)


print("k" in (name and month and year))

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.


name="abcd"
month="efgh"
year="ijkl"

"k" in (name and month and year)

True

(name and month and year)

'ijkl'





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'.

 "k" in (name and month and year)
> True

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?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Grant Edwards
On 2013-06-14, Nick the Gr33k  wrote:

> I started another thread

no kidding.

> because the last one was !@#$'ed up by irrelevant replies and was
> difficult to jeep track.
>
> >>> name="abcd"
> >>> month="efgh"
> >>> year="ijkl"
>
> >>> print(name or month or year)
> 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.

> >>> print("k" in (name and month and year))
> 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.

>>> name="abcd"
>>> month="efgh"
>>> year="ijkl"
>>> 
>>> "k" in (name and month and year)
True
>>> (name and month and year)
'ijkl'
>>> 

> how can 'k' contained within 'abcd' ?

It doesn't

-- 
Grant Edwards   grant.b.edwardsYow! Am I having fun yet?
  at   
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Robert Kern

On 2013-06-14 10:50, Nick the Gr33k wrote:

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

 >>> name="abcd"
 >>> month="efgh"
 >>> year="ijkl"

 >>> print(name or month or year)
abcd

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

 >>> print("k" in (name and month and year))
True

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

 >>> print(name and month and year)
ijkl

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

 >>> print("k" in (name and month and year))
True
 >>>

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

--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Fábio Santos
On 14 Jun 2013 10:59, "Nick the Gr33k"  wrote:
>
> I started another thread because the last one was !@#$'ed up by
irrelevant replies and was difficult to jeep track.
>
> >>> name="abcd"
> >>> month="efgh"
> >>> year="ijkl"
>
> >>> print(name or month or year)
> abcd
>
> Can understand that, it takes the first string out of the 3 strings that
has a truthy value.
>
> >>> print("k" in (name and month and year))
> True
>
> No clue. since the expression in parenthesis returns 'abcd' how can 'k'
contained within 'abcd' ?
>
> >>> print(name and month and year)
> ijkl
>
> Seems here is returning the last string out of 3 strings, but have no
clue why Python doing this.
>
> >>> print("k" in (name and month and year))
> True
> >>>
>
> 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.

You have been explained with both words, links and examples and now you
dismiss the previous post because it was "difficult to jeep track"?

Anyway. Search for "short circuit logic". Short circuit logic is why python
does it that way.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-14 Thread Nick the Gr33k
I started another thread because the last one was !@#$'ed up by 
irrelevant replies and was difficult to jeep track.


>>> name="abcd"
>>> month="efgh"
>>> year="ijkl"

>>> print(name or month or year)
abcd

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


>>> print("k" in (name and month and year))
True

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


>>> print(name and month and year)
ijkl

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


>>> print("k" in (name and month and year))
True
>>>

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.
--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list