Proper way to assert type of variable in Python
- by Morlock
In using a function, I wish to ensure that the type of the variables are as expected. How to do it right?
Here is an example fake function trying to do just this before going on with its role:
def my_print(text, begin, end):
"""Print text in UPPER between 'begin' and 'end' in lower
"""
for i in (text, begin, end):
assert type(i) == type("")
out = begin.lower() + text.upper() + end.lower()
print out
Is this approach valid? Should I use something else than
type(i) == type("")
? Should I use try/except instead?
Thanks pythoneers