Proper way to assert type of variable in Python

Posted by Morlock on Stack Overflow See other posts from Stack Overflow or by Morlock
Published on 2010-04-07T01:51:10Z Indexed on 2010/04/07 1:53 UTC
Read the original article Hit count: 327

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about python

Related posts about assert