3

Why is the self.year twice? I am having trouble to find out the logic of the line. Can some one help me with this?

return (self.year and self.year == date.year or True)

I am going through http://www.openp2p.com/pub/a/python/2004/12/02/tdd_pyunit.html and encountered the line ... And of course I have no problem understanding and, or, nor, xor, xnor, or any boolean expression. But I am confused by the way it has been used here..

:-)

8
  • 2
    Boolean operations Commented Jan 27, 2014 at 9:54
  • I know they are but I want to know the logic of having self.year twice. @devnull Commented Jan 27, 2014 at 9:57
  • @devnull Python 2.5 is quite old has not been actively developed for some years. Here's the link to more recent (Py3) docs: docs.python.org/3/library/… Commented Jan 27, 2014 at 9:58
  • Actually I am not having trouble with and & or. Have dealt with them for long time in Digital Logic. I want to know what the logic is? Commented Jan 27, 2014 at 10:04
  • This is a bit weird, the expression seems to always return True. Commented Jan 27, 2014 at 10:11

4 Answers 4

1

The order of evaluation matters (see here). The code:

return self.year and self.year == date.year

could be rewritten:

return self.year and (self.year == date.year)

Or, in full:

if self.year:
    if self.year == date.year:
        return True
    else:
        return False
return False # Actually return self.year, but usually a boolean is intended in this sort of situation

However, the expression you posted will always evaluate to True because of the or True at the end. Using parenthesis to show evaluation order:

return (self.year and (self.year == date.year)) or True
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you.. I got it now.. And of course the meaning of and , or hasn't change ever.. Right? :-)
1

This line will always return true because you are doing an or with a true value.

Apart from that, first it checks if self.year is not None or False, than checks if self.year is equal to date.year.

Comments

1
return (self.year and self.year == date.year or True)

yes, it is required to include self.year twice. Because it will check that there is something self.year defined and if it's not defined it will give an error that there is no thing like "self.year" i.e. it's undefined. As it approves that self.year is something which is defined than it will check for it's value.

Comments

1
return (self.year and self.year == date.year or True)

In this case, first there is an existence check on self.year which will return (i assume) None (in python==False) if it does not exist. after that it is compared to date.year

I think it is a bit redundant, because the comparison would return false anyway if self.year did not exist.

As commented by Bereal on your question, the last or True is weird indeed, cause that seems to cause the total expression to always return True

general Note: In python boolean expressions are always evaluated 'in order of appearence'.

2 Comments

in order of appearence or there is any precedence ??
A and B and C is equivalent to (A and B) and C. 'A==1 or 2' is not A==(1 or 2) but (A==1) or 2 efectively: always true unless A==0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.