3

I have variable passed in which can be a string or integer passed in. For example it can be '123' or 123. Additionally, it could be a string like 'N/A', and in this case I want to replace it with 0.

I have been trying to do something like this:

our_value = int(our_value) if our_value.isdigit() else 0

The issue is when our_value is an integer it has no method isdigit. If it's a string this will work fine.

How can I handle both cases where it can be a integer or string?

2
  • 3
    This doesn't answer your question, but you might be better off redesigning your program so that the function only needs to operate on one type of argument. You might need to write two functions -- one for ints and one for strings -- but a little extra verbosity might save you some laborious type checking.
    – Kevin
    Commented Mar 25, 2019 at 12:53
  • Yea, that would be ideal, unfortunately I don't have control over the system which sends data to the program, so need to handle both cases. Commented Mar 25, 2019 at 12:56

4 Answers 4

6

This will work as well

try:
    our_value = int(our_value)
except ValueError:
   our_value = 0
1
  • 1
    You were quicker than me. :-)
    – JohanL
    Commented Mar 25, 2019 at 12:50
3

To avoid double conversions it is possible to use a try/except construction such as:

try:
    our_value = int(our_value)
except ValueError:
    our_value = 0

In this case, we try to coerce the value to an integer. This will be successful if we have an integer already, or a string that can be interpreted as an integer.

Other strings will fall into our except case and thereby set to 0.

1

If you don't want to write any exception you can go with this code:

our_value = '123' # It can be 'N/A' or 123
if type(our_value ) is str:
    if our_value .isdigit():
        our_value  = int(our_value)
    else:
        our_value  = 0 
print(our_value)

If the type is string and its a number then we can apply int() function. If it is int and not a number then it's 'N/A' and converted to 0. If the value is int then there is no need for any conversions.

0

This will work:

our_value = int(our_value) if str(our_value).isdigit() else 0

If our_value will be integer, it would be converted to the string first and if it contains only digits, than it would be converted back to integer.

6
  • This rubs me the wrong way. Turing an integer to a string to an integer feels like one conversion too many.
    – JohanL
    Commented Mar 25, 2019 at 12:46
  • 1
    I notice that if our_value is initially the integer -10, then the result of this statement is 0 and not -10. This may be a problem.
    – Kevin
    Commented Mar 25, 2019 at 12:47
  • 1
    Arguable. If the OP wants the function to work on "integers", then it should work on all integers, yes? OP did not specifically say that the integers could be negative, but he also did not say the integers could be odd, and it would be weird to post an answer that works only on even integers.
    – Kevin
    Commented Mar 25, 2019 at 12:49
  • 1
    @Kevin OP does provide a code block which is already includes is str.isdigit() so I assume OP knows why OP needs it
    – krasu
    Commented Mar 25, 2019 at 12:50
  • 1
    Also notice that '⑴①'.isdigit() == True, hence a unexpected ValueError might be raised.
    – Arnie97
    Commented Mar 25, 2019 at 13:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.