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

Νικόλαος Κούρας support at superhost.gr
Thu Jun 13 01:36:42 EDT 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.

First i wan tot thank you for taking the time to explain to me (the 
languages examples to explai encode-decode was really great as well)


So, baring in ming your explanation:

if '=' not in ( name or month or year )

first eval the expr. The expr will result to return the first object 
that has a truthy values.

in this particular example all 3 strings are truthies because they all 
contain characters inside them, the one user submitted when hitting the 
submit button

so that would turn to if '=' not in ( name ).
Allright, but what i wanted to check is if the char '=' ain't contained 
in both 3 strings not for the 1st string(name) which will always be true.

So i guess having it like this "if '=' not in ( name or month or year )" 
  is wrong?


b) Also what "if '=' not in ( name and month and year )" returns in the 
round brackets since in my case all the strings have values therefore 
they are true, which string get returned?




More information about the Python-list mailing list