Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python isdigit() and None
#4
In circumstances like this where the value may be None (or an empty string, list, etc.), one option is to use and to make sure there is a value before trying isdigit().

So, instead of:
if presets.get('last').isdigit()
Do this:
if presets.get('last') and presets.get('last').isdigit()
This works because Python first evaluates if presets.get('last'). If there is no value, the condition is False (because False and x is False no matter what x is), so Python doesn't even evaluate beyond the "and".

Also, since the isdigit() method returns True or False, there is no need for the comparison operator:
if x.isdigit():  # accomplishes the same thing as if x.isdigit() == True:
Gribouillis likes this post
Reply


Messages In This Thread
Python isdigit() and None - by samtal - May-06-2021, 12:59 PM
RE: Python isdigit() and None - by perfringo - May-06-2021, 01:40 PM
RE: Python isdigit() and None - by Gribouillis - May-06-2021, 01:50 PM
RE: Python isdigit() and None - by GOTO10 - May-06-2021, 02:04 PM
RE: Python isdigit() and None - by samtal - May-06-2021, 02:54 PM
RE: Python isdigit() and None - by ndc85430 - May-06-2021, 03:06 PM
RE: Python isdigit() and None - by Gribouillis - May-06-2021, 05:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Filter and str.isdigit producing an error tester_V 5 1,963 Aug-12-2022, 07:50 AM
Last Post: Gribouillis
  str.isdigit() vs str.isnumeric() Skaperen 4 6,317 Jun-14-2019, 01:54 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020