User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Regular expression that turns into a python syntax question

Tue Jul 03, 2018 8:13 pm

I am using regular expression to match a number at the end of a string using '[0-9]$'. It works perfectly except when the string is empty (didn't get a response from serial port). See the following snippet:

Code: Select all

m=re.search('[0-9]$','A7')
print(m.group(0))
I read from Python doc that the m object evaluates into True if there are matches and it is None if there are no matches:

https://docs.python.org/3/library/re.html#match-objects

Code: Select all

>>> if m:
	print('match')
My question is, since m is a reference to an object, how does m get its True value in the if statement? I sifted through all methods of m but failed to find a function that can evaluate into True. So my question is, what is the mechanism to produce True when the object reference m is placed in the if statement? Thanks.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

ericcooper
Posts: 123
Joined: Sat Apr 08, 2017 6:23 pm

Re: Regular expression that turns into a python syntax question

Tue Jul 03, 2018 8:28 pm

Any non-zero / non-empty value is treated as True for "if" statements and other conditionals.

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Regular expression that turns into a python syntax question

Tue Jul 03, 2018 8:31 pm

The re.search function returns a match object on success, none on failure.

https://www.tutorialspoint.com/python/p ... .htm/quote]

Instead of testing for true test, in this instance, for m being an instantiated object or m being a not None:

Code: Select all

if (m is not None):
    DoTheThing
Last edited by Idahowalker on Tue Jul 03, 2018 8:40 pm, edited 3 times in total.
Without knowing why you are deleting my postings, I will not know how...

User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Regular expression that turns into a python syntax question

Tue Jul 03, 2018 8:34 pm

ericcooper wrote:
Tue Jul 03, 2018 8:28 pm
Any non-zero / non-empty value is treated as True for "if" statements and other conditionals.
I see. That's why. It makes sense. Thanks.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Regular expression that turns into a python syntax question

Tue Jul 03, 2018 8:40 pm

Idahowalker wrote:
Tue Jul 03, 2018 8:31 pm
The re.search function returns a match object on success, none on failure.

https://www.tutorialspoint.com/python/p ... ssions.htm
Instead of testing for true test for

Code: Select all

if (m is not None):
Fixed your link. Thanks. The answer ericcooper provided was what I was looking for, reason that m is considered as True.

Can you explain any advantage of testing None? The tutorials point link and python doc both test True.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Regular expression that turns into a python syntax question

Tue Jul 03, 2018 9:10 pm

liudr wrote:
Tue Jul 03, 2018 8:40 pm
Fixed your link. Thanks. The answer ericcooper provided was what I was looking for, reason that m is considered as True.

Can you explain any advantage of testing None? The tutorials point link and python doc both test True.
Just my preference. An Object, which has State, can be True or False or Null (None). To me, Null (None), which represents State, is not a True or a False. An Object, which has State, can contain a False. To test the Object, not the value of the Object, for True, when the Object contains False will return True. The if statement as it was written works by testing for 'something' being there. That 'something' is or is not there is not the same as True or False; thus, I set a differentiation between True, False, and None (Null).

For example, I have a class that I instantiate, give State, as an Object. The Object, from time to time will return an I/O error. When the Object returns an I/O error, the State of the Object remains but is broken. If I was to test the State of the Object for True, the True test would pass but, for this Object, the Object fails to operate and the Object Methods, being called, fail to run. So, if the class Object returns an error, the error handler sets the Object State to None, not False. When the Function that uses the class Object begins its run, the Function tests to see if the Object is instantiated, has State. That the Object, with an error, has been set to None (State Removed), a test for the Object being None is done. If the class Object is a None the Object can be re-instantiated and the Function runs the Objects Methods like nothing happened.
Last edited by Idahowalker on Tue Jul 03, 2018 9:25 pm, edited 1 time in total.
Without knowing why you are deleting my postings, I will not know how...

User avatar
Paeryn
Posts: 2616
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Regular expression that turns into a python syntax question

Tue Jul 03, 2018 9:24 pm

liudr wrote:
Tue Jul 03, 2018 8:40 pm
Can you explain any advantage of testing None? The tutorials point link and python doc both test True.
Other than explicitly testing if m is not None: being overly verbose there is no difference to testing if m:
I would assume the bytecode emitter optimizes the two methods to the same sequence, if not then then the verbose method probably takes ever so slightly (as in you wouldn't be able to tell unless doing a huge number of such tests) longer to execute.


See posts below for why what I wrote here is wrong, I didn't have my programming head on when I wrote this. :oops:
Last edited by Paeryn on Thu Jul 05, 2018 10:14 pm, edited 1 time in total.
She who travels light — forgot something.

User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Regular expression that turns into a python syntax question

Thu Jul 05, 2018 4:19 pm

Thanks for taking the time to explain. Maybe one will be a more future-proof way than the other? If I can bet that .match() will always be returning None in future versions, it seems to be a more accurate way to do the if-statement. I'm frankly not sure what None represents, coming from a C/C++ background :roll:

I'll read about None and probably use it once I feel comfortable with it :)
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

hippy
Posts: 5623
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Regular expression that turns into a python syntax question

Thu Jul 05, 2018 6:32 pm

Paeryn wrote:
Tue Jul 03, 2018 9:24 pm
Other than explicitly testing if m is not None: being overly verbose there is no difference to testing if m:
The two are not quite the same, though if only None or True values of 'm' exist they would be equivalent.

"if m is not None:" is equivalent to "if TrueFalseOrNone(m) != None:"

"if m:" is equivalent to "if TrueFalseOrNone(m) == True:"

The first one will evaluate as true when m==False and/or m==True, the second only when "m==True".

User avatar
Paeryn
Posts: 2616
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Regular expression that turns into a python syntax question

Thu Jul 05, 2018 10:11 pm

hippy wrote:
Thu Jul 05, 2018 6:32 pm
Paeryn wrote:
Tue Jul 03, 2018 9:24 pm
Other than explicitly testing if m is not None: being overly verbose there is no difference to testing if m:
The two are not quite the same, though if only None or True values of 'm' exist they would be equivalent.

"if m is not None:" is equivalent to "if TrueFalseOrNone(m) != None:"

"if m:" is equivalent to "if TrueFalseOrNone(m) == True:"

The first one will evaluate as true when m==False and/or m==True, the second only when "m==True".
That's a point, didn't think about the case where m = False. Thanks hippy, I'll edit my previous post.

It would also affect the case where m is any of the false values including 0, an empty string '', an empty tuple (), an empty list [], an object which has a method __bool__() which returns False, there may be a few other cases too*. These all evaluate to False when used in boolean tests but are definitely not None.

* Full list of what is considered to be False https://docs.python.org/3.5/library/std ... ight=truth
She who travels light — forgot something.

hippy
Posts: 5623
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Regular expression that turns into a python syntax question

Fri Jul 06, 2018 11:37 am

I was intrigued as to what "if m is not None:" actually parses as because I have never used it. Does operator precedence have the "not" tied to the previous "is" or to the following expression? Is it ...

if not( is( m, None ) )

or

if is( m, not( None ) )

That ambiguity is probably why I have never used it; working on the principle that if one cannot figure something out without consulting the documentation or testing it then it's best not to use it.

User avatar
Paeryn
Posts: 2616
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Regular expression that turns into a python syntax question

Fri Jul 06, 2018 5:24 pm

hippy wrote:
Fri Jul 06, 2018 11:37 am
I was intrigued as to what "if m is not None:" actually parses as because I have never used it. Does operator precedence have the "not" tied to the previous "is" or to the following expression? Is it ...

if not( is( m, None ) )

or

if is( m, not( None ) )

That ambiguity is probably why I have never used it; working on the principle that if one cannot figure something out without consulting the documentation or testing it then it's best not to use it.
In Python is tests for identity rather than equivalence i.e. that the two sides are references to the exact same object, is not is testing that they aren't the same object, it is one operator so would be equivalent to

Code: Select all

if isnot(m,None)
None is a special object in that there is only ever one instance of it so it is a very fast comparison to check if an object is None or is not None.
She who travels light — forgot something.

Return to “Python”