943,836 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1290
  • Python RSS
Aug 14th, 2009
0

How does python evaluate conditional statements?

Expand Post »
(a | b):
For what I have read in other forum post, if a is true, python doesn't evaluate b an returns a.

(a & b):
What happens with logical and? If a is false, does python evaluates b or not?

Thanks for your help.
Last edited by nunos; Aug 14th, 2009 at 4:30 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
nunos is offline Offline
46 posts
since Aug 2009
Aug 14th, 2009
1

Re: How does python evaluate conditional statements?

Click to Expand / Collapse  Quote originally posted by nunos ...
(a & b):
What happens with logical and? If a is false, does python evaluates b or not?
No. Here's the code I just used to check this:
python Syntax (Toggle Plain Text)
  1. >>> def a():
  2. ... print 'a'
  3. ... return False
  4. ...
  5. >>> def b():
  6. ... print 'b'
  7. ... return True
  8. ...
  9. >>> a() and b()
  10. a
  11. False
  12. >>> b() and a()
  13. b
  14. a
  15. False
  16. >>>
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Aug 14th, 2009
0

Re: How does python evaluate conditional statements?

The reason I asked about the evaluation of the logical and was because of this error I keep getting.

Here's the code:
python Syntax (Toggle Plain Text)
  1. import re
  2.  
  3. m = "03-AA-12"
  4.  
  5. x = re.compile("[A-Z]{2,2}-[0-9]{2,2}-[0-9]{2,2}")
  6.  
  7. objx = x.match(m)
  8.  
  9. if (objx != None) & (objx.group() == m):
  10. print "yes"
  11. else:
  12. print "no"

Here's the error:
Traceback (most recent call last):
  File "D:\Workspace\py.LAB\src\lab.py", line 9, in <module>
    if (objx != None) & (objx.group() == m):
AttributeError: 'NoneType' object has no attribute 'group'

According to what was mentioned before, I can't understand why this error occurs.

Python should only evaluate objx.group() == m if objx != None is True, since it is False it should not evaluate it and this error would not occur.

To overcome this I nested two if statements, like so:
python Syntax (Toggle Plain Text)
  1. if objx != None:
  2. if objx.group() == m:
  3. print "yes"

Even though this last piece of code works I still would like to know why the error occurs.

Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
nunos is offline Offline
46 posts
since Aug 2009
Aug 14th, 2009
0

Re: How does python evaluate conditional statements?

The logical and in python is the and operator, not the & which is bitwise and.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Aug 14th, 2009
0

Re: How does python evaluate conditional statements?

The logical and in python is the and operator, not the & which is bitwise and.
Thanks. I changed '&' to 'and' and it now works as it should.

Cheers.
Last edited by nunos; Aug 14th, 2009 at 7:32 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
nunos is offline Offline
46 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: self programming code
Next Thread in Python Forum Timeline: print string w/o translating special characters





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC