This will distinguish ints from str and str to int without requiring you to know the type in advance. What he does is define to call ((str) or (int) on (a / b). Inline 'a if b else c' is equivalent to the ternary operator: (which you may know).
a = '1' a_res = (str if type(a) == type(1) else int)(a) print(type(a_res)) b = 1 b_res = (str if type(b) == type(1) else int)(b) print(type(b_res))
It produces:
>>> <class 'int'> <class 'str'>
As you can see, the same code is used to convert both a and b.
source share