title: How to avoid type checking arguments to Python function
body: <p>I'm creating instances of a class Foo, and I'd like to be able to instantiate these in a general way from a variety of types. You can't pass Foo a dict or list. Note that Foo is from a 3rd party code base - I can't change Foo's code.</p>
  
  <p>I know that type checking function arguments in Python is considered bad form. Is there a more Pythonic way to write the function below (i.e. without type checking)?</p>
  
  <pre><code>def to_foo(arg):
    if isinstance(arg, dict):
      return dict([(key,to_foo(val)) for key,val in arg.items()])
    elif isinstance(arg, list):
      return [to_foo(i) for i in arg]
    else:
      return Foo(arg)
  </code></pre>
  
  <p>Edit: Using try/except blocks is possible. For instance, you could do:</p>
  
  <pre><code>def to_foo(arg):
    try:
      return Foo(arg)
    except ItWasADictError:
      return dict([(key,to_foo(val)) for key,val in arg.items()])
    except ItWasAListError:
      return [to_foo(i) for i in arg]
  </code></pre>
  
  <p>I'm not totally satisfied by this for two reasons: first, type checking seems like it addresses more directly the desired functionality, whereas the try/except block here seems like it's getting to the same place but less directly. Second, what if the errors don't cleanly map like this? (e.g. if passing either a list or dict throws a TypeError)</p>
  
  <p>Edit: a third reason I'm not a huge fan of the try/except method here is I need to go and find what exceptions Foo is going to throw in those cases, rather than being able to code it up front.</p>