简体   繁体   中英

Is there any way to write it better?

i have a code like this:

page_number = re.sub('[^0-9]', '', total_matches)
page_number = int(page_number) + 1

Is there any way to write it better? I mean something like that:

x = 5
x += 5

一班轮:

page_number = int(re.sub('[^0-9]', '', total_matches)) + 1

Change the place where int is called.

page_number = int(re.sub('[^0-9]', '', total_matches))
page_number += 1

If it isn't guaranteed that there is always a page number you can catch the exception.

try:    
    page_number = int(re.sub('[^0-9]', '', total_matches))
except ValueError:
    page_number = 0
page_number += 1

Now it's longer but more robust.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM