Basically the argument that foo()
expects should be passed as int
, but there is a possibility that someone will pass it as str
(which is also valid if str
can be converted to int
). This is what I came up with:
def foo(input_argument):
func_name = 'foo'
if type(input_argument) is not int or type(input_argument) is not str:
print(
'%s: "input_argument" expects int/str, not %s' % (
func_name,
type(input_argument)
)
)
return None
try:
input_argument= int(input_argument)
except:
print(
'%s: "input_argument" expects number in str/int format' % func_name
)
return None
Is there something that is built-in which could simplify this in a more pythonic way?
Edit: boolean type should be treated as invalid