I'm sure I read this somewhere recently, but my scratchy memory and command of Google can't bring it back to me. Is there a Python idiom for accepting either a file name or a file object as a function parameter? The closest I can get is this;


def my_function(file_name_or_object):

    try:

        open(file_name_or_object)

    except TypeError:

        file = file_name_or_object

    return file

Any improvements on this are more than welcome.