(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.
(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.
(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:
>>> def a():
... print 'a'
... return False
...
>>> def b():
... print 'b'
... return True
...
>>> a() and b()
a
False
>>> b() and a()
b
a
False
>>>
The reason I asked about the evaluation of the logical and was because of this error I keep getting.
Here's the code:
import re
m = "03-AA-12"
x = re.compile("[A-Z]{2,2}-[0-9]{2,2}-[0-9]{2,2}")
objx = x.match(m)
if (objx != None) & (objx.group() == m):
print "yes"
else:
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:
if objx != None:
if objx.group() == m:
print "yes"
Even though this last piece of code works I still would like to know why the error occurs.
Thanks.
The logical and in python is the and
operator, not the &
which is bitwise and.
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.