Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A certainl part of an if() structure never gets executed.

Reply
Thread Tools

A certainl part of an if() structure never gets executed.

 
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-13-2013
On 13/6/2013 4:55 πμ, Steven D'Aprano wrote:
> On Wed, 12 Jun 2013 14:17:32 +0300, Νικόλαος Κούρας wrote:
>
>> doesn't that mean?
>>
>> if '=' not in ( name and month and year ):
>>
>> if '=' does not exists as a char inside the name and month and year
>> variables?
>>
>> i think it does, but why it fails then?

>
> No. Python is very close to "English-like", but not exactly, and this is
> one of the easiest places to trip.
>
> In English:
>
> "the cat is in the box or the cupboard or the kitchen"
>
> means:
>
> "the cat is in the box, or the cat is in the cupboard, or the cat is in
> the kitchen".
>
>
> But that is not how Python works. In Python, you have to say:
>
> cat in box or cat in cupboard or cat in kitchen
>
>
> Although this will work as well:
>
> any(cat in place for place in (box, cupboard, kitchen))
>
>
> In Python, an expression like this:
>
> cat in (box or cupboard or kitchen)
>
>
> has a completely different meaning. First, the expression in the round
> brackets is evaluated:
>
> (box or cupboard or kitchen)
>
>
> and then the test is performed:
>
> cat in (result of the above)
>
>
> The expression (box or cupboard or kitchen) means "return the first one
> of box, cupboard, kitchen that is a truthy value, otherwise the last
> value". Truthy values are those which are considered to be "like True":
>
> truthy values:
>
> - True
> - object()
> - numbers apart from zero
> - non-empty strings
> - non-empty lists
> - non-empty sets
> - non-empty dicts
> - etc.
>
> falsey:
>
> - False
> - None
> - zero (0, 0.0, Decimal(0), Fraction(0), etc.)
> - empty string
> - empty list
> - empty set
> - empty dict
> - etc.
>
> (Can you see the pattern?)
>
>
> So you can experiment with this yourself:
>
> 42 or 23 or "foo"
> => the first object is truthy, so it is returned
>
> 0 or 23 or "foo"
> => the first object is falsey, and the second object is truthy,
> so it is returned
>
> 0 or [] or "foo"
> => the first two objects are falsey, so the third is returned
>
>
> The "and" operator works in a similar fashion. Experiment with it and see
> how it works for yourself.


I read yours psots many times,all of them, tryign to understand them.


if '-' not in ( name and month and year ):
cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM
clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) =
%s ORDER BY lastvisit ASC''', (name, month, year) )
elif '-' not in ( name and year ):
cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM
clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit
ASC''', (name, year) )
elif '-' not in ( month and year ):
cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit) = %s and
YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
elif '-' not in year:
cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit) = %s ORDER
BY lastvisit ASC''', year )

======================

i just want 4 cases to examine so correct execute to be run:

i'm reading and reading and reading this all over:

if '-' not in ( name and month and year ):

and i cant comprehend it.

While it seems so beutifull saying:

if character '-' aint contained in string name , neither in string month
neither in string year.

But it just doesn't work like this.

Since ( name and month and year ) are all truthy values, what is
returned by this expression to be checked if it cotnains '=' within it?


 
Reply With Quote
 
 
 
 
Sibylle Koczian
Guest
Posts: n/a
 
      06-13-2013
Am 13.06.2013 09:11, schrieb Νικόλαος Κούρας:
> On 13/6/2013 4:55 πμ, Steven D'Aprano wrote:
>>
>> The "and" operator works in a similar fashion. Experiment with it and see
>> how it works for yourself.

>
> I read yours psots many times,all of them, tryign to understand them.
>


But you didn't do what he recommended, did you? And it's really the best
or possibly the only way to understanding.

Try it out in the interactive Shell, using Stevens examples connected
with 'and' instead of 'or'.

Sibylle




 
Reply With Quote
 
 
 
 
rusi
Guest
Posts: n/a
 
      06-13-2013
On Jun 12, 8:20*pm, Zero Piraeus <(E-Mail Removed)> wrote:
> :
>
> On 12 June 2013 10:55, Neil Cerutti <(E-Mail Removed)> wrote:
>
>
>
> > He's definitely trolling. I can't think of any other reason to
> > make it so hard to kill-file himself.

>
> He's not a troll, he's a help vampire:
>
> *http://slash7.com/2006/12/22/vampires/
>
> ... a particularly extreme example, I'll admit


Thanks for that term.

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      06-13-2013
On 2013-06-12, Zero Piraeus <(E-Mail Removed)> wrote:
> On 12 June 2013 10:55, Neil Cerutti <(E-Mail Removed)> wrote:
>>
>> He's definitely trolling. I can't think of any other reason to
>> make it so hard to kill-file himself.

>
> He's not a troll, he's a help vampire:
>
> http://slash7.com/2006/12/22/vampires/
>
> ... a particularly extreme example, I'll admit: his lack of
> consideration for others apparently knows no bounds. The email thing
> is just another aspect of that.


He's also changed his NNTP-Posting-Host, just yesterday, along
with at least three changes in From address, and one change in
Reply-To.

And to start with he came here with an obvious troll name.

--
Neil Cerutti
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      06-13-2013
In article
<(E-Mail Removed)>,
rusi <(E-Mail Removed)> wrote:

> On Jun 12, 8:20*pm, Zero Piraeus <(E-Mail Removed)> wrote:
> > :
> >
> > On 12 June 2013 10:55, Neil Cerutti <(E-Mail Removed)> wrote:
> >
> >
> >
> > > He's definitely trolling. I can't think of any other reason to
> > > make it so hard to kill-file himself.

> >
> > He's not a troll, he's a help vampire:
> >
> > *http://slash7.com/2006/12/22/vampires/
> >
> > ... a particularly extreme example, I'll admit

>
> Thanks for that term.


Yeah. I've never heard that before, but it's perfect.
 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-13-2013
On 13/6/2013 3:22 μμ, Sibylle Koczian wrote:
> Am 13.06.2013 09:11, schrieb Νικόλαος Κούρας:
>> On 13/6/2013 4:55 πμ, Steven D'Aprano wrote:
>>>
>>> The "and" operator works in a similar fashion. Experiment with it and
>>> see
>>> how it works for yourself.

>>
>> I read yours psots many times,all of them, tryign to understand them.
>>

>
> But you didn't do what he recommended, did you? And it's really the best
> or possibly the only way to understanding.
>
> Try it out in the interactive Shell, using Stevens examples connected
> with 'and' instead of 'or'.


I try and try to work it out but i can't understand it even in theory.

==================

if '-' not in ( name and month and year ):
cur.execute( '''SELECT * FROM works WHERE clientsID =
(SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and
YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (name, month, year) )
elif '-' not in ( name and year ):
cur.execute( '''SELECT * FROM works WHERE clientsID =
(SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER
BY lastvisit ASC''', (name, year) )
elif '-' not in ( month and year ):
cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit)
= %s and YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
elif '-' not in year:
cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit) =
%s ORDER BY lastvisit ASC''', year )

======================

i just want 4 cases to examine so correct execute to be run:

i'm reading and reading and reading this all over:

if '-' not in ( name and month and year ):

and i cant comprehend it.

While it seems so beautiful saying:

if character '-' ain't contained in string name , neither in string
month neither in string year.

But it just doesn't work like this.

Since ( name and month and year ) are all truthy values, what is
returned by this expression to be checked if it cotnains '=' within it?



 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-13-2013
On 13/6/2013 12:16 πμ, Sibylle Koczian wrote:
> Am 12.06.2013 22:00, schrieb Νικόλαος Κούρας:
>> On 12/6/2013 10:48 μμ, Sibylle Koczian wrote:
>>>> if '=' not in ( name and month and year ):
>>>> i understand: if '=' not in name AND '=' not in month AND '=' not in
>>>> year
>>>>
>>> Wrong. The "'=' not in (...)" first evaluates the expression in
>>> parentheses, that's what parentheses are for. And then it looks for '='
>>> in the result. And that result is just one of the three values, MRAB
>>> told you which one.

>>
>> okey first the expression eval:
>>
>> ( name and month and year ) = ( name=True and month=True and year=True )
>>

> No. Read MRABs post, he explains it. Or work through the tutorial. This
> would be right in another language, but not in Python.
>
> If this expression would really evaluate to True or False, you
> definitely couldn't search for any character in the result.
>
> As it is, it evaluates to a string or to None, but searching for '=' in
> that string doesn't give the result you think it does.


(name and month and year) is a Boolean expression, correct?

It will return True if all three are True and False otherwise. I cannot
use it the way i desire regardless of how logical i think it looks.

Basically its just like i'am saying:


if "-" in True:
or
if "-" in False:

Obviously when i write it this way you can see it makes no sense.

======================
But (name or month or year) is also a Boolean expression.

It will return the first of the three depending which value comes first
as truthy.
it will return False if none of the three are False.

So how am i going to write it that?

if '-' not in name and '-' not in month and '-' not in year: ??????


 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-13-2013
if '-' not in name + month + year:
cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM
clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) =
%s ORDER BY lastvisit ASC''', (name, month, year) )
elif '-' not in name + year:
cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM
clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit
ASC''', (name, year) )
elif '-' not in month + year:
cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit) = %s and
YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
elif '-' not in year:
cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit) = %s ORDER
BY lastvisit ASC''', year )


This finally worked!
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      06-14-2013
On Thu, 13 Jun 2013 17:26:18 +0300, Νικόλαος Κούρας wrote:

> i just want 4 cases to examine so correct execute to be run:
>
> i'm reading and reading and reading this all over:
>
> if '-' not in ( name and month and year ):
>
> and i cant comprehend it.


Don't just read it. Open the interactive interpreter and test it.

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

print(name and month and year)

If you run that, you will see what the result of
(name and month and year) is. Now, ask yourself:

"k" in (name and month and year)

True or false? Check your answer:

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



> While it seems so beautiful saying:
>
> if character '-' ain't contained in string name , neither in string
> month neither in string year.
>
> But it just doesn't work like this.


Correct. It doesn't work that way.

> Since ( name and month and year ) are all truthy values, what is
> returned by this expression to be checked if it cotnains '=' within it?


Stop asking these questions. Try it for yourself and learn for yourself.
You have a Python interactive interpreter. Try things, and see what they
do. Read the documentation. THEN, and ONLY after you have done these
things, should you ask for help.


--
Steven
 
Reply With Quote
 
Nick the Gr33k
Guest
Posts: n/a
 
      06-14-2013
On 14/6/2013 4:14 πμ, Steven D'Aprano wrote:
> On Thu, 13 Jun 2013 17:26:18 +0300, Νικόλαος Κούρας wrote:
>
>> i just want 4 cases to examine so correct execute to be run:
>>
>> i'm reading and reading and reading this all over:
>>
>> if '-' not in ( name and month and year ):
>>
>> and i cant comprehend it.

>
> Don't just read it. Open the interactive interpreter and test it.
>
> name = "abcd"
> month = "efgh"
> year = "ijkl"
>
> print(name and month and year)
>
> If you run that, you will see what the result of
> (name and month and year) is. Now, ask yourself:
>
> "k" in (name and month and year)
>
> True or false? Check your answer:
>
> print("k" in (name and month and year))



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

--
What is now proved was at first only imagined!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
semple code accept never executed fakessh C Programming 11 09-15-2012 05:46 AM
Using callback, I fill dorpdownlist without postback but it gets empty when a postback is executed Fabio Mastria ASP .Net 5 01-30-2008 09:07 PM
server event never/always executed phil ASP .Net Web Controls 1 06-06-2006 12:00 PM
ORIGINAL CODE GETS EXECUTED NOT THE NEW CODE - ASCX sk ASP .Net 1 04-19-2006 11:50 AM
Page_Unload gets executed twice chefo@bulgaria.com ASP .Net 0 01-21-2005 01:44 PM



Advertisments