How to handle "duck typing" in Python?

Question:

I usually want to keep my code as generic as possible. I’m currently writing a simple library and being able to use different types with my library feels extra important this time.

One way to go is to force people to subclass an “interface” class. To me, this feels more like Java than Python and using issubclass in each method doesn’t sound very tempting either.

My preferred way is to use the object in good faith, but this will raise some AttributeErrors. I could wrap each dangerous call in a try/except block. This, too, seems kind of cumbersome:

def foo(obj):
    ...
    # it should be able to sleep
    try:
        obj.sleep()
    except AttributeError:
        # handle error
    ...
    # it should be able to wag it's tail
    try:
        obj.wag_tail()
    except AttributeError:
        # handle this error as well

Should I just skip the error handling and expect people to only use objects with the required methods? If I do something stupid like [x**2 for x in 1234] I actually get a TypeError and not a AttributeError (ints are not iterable) so there must be some type checking going on somewhere — what if I want to do the same?

This question will be kind of open ended, but what is the best way to handle the above problem in a clean way? Are there any established best practices? How is the iterable “type checking” above, for example, implemented?

Edit

While AttributeErrors are fine, the TypeErrors raised by native functions usually give more information about how to solve the errors. Take this for example:

>>> ['a', 1].sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()

I’d like my library to be as helpful as possible.

Asked By: André Laszlo

||

Answers:

Good question, and quite open-ended. I believe typical Python style is not to check, either with isinstance or catching individual exceptions. Cerainly, using isinstance is quite bad style, as it defeats the whole point of duck typing (though using isinstance on primitives can be OK — be sure to check for both int and long for integer inputs, and check for basestring for strings (base class of str and unicode). If you do check, you hould raise a TypeError.

Not checking is generally OK, as it typically raises either a TypeError or AttributeError anyway, which is what you want. (Though it can delay those errors making client code hard to debug).

The reason you see TypeErrors is that primitive code raises it, effectively because it does an isinstance. The for loop is hard-coded to raise a TypeError if something is not iterable.

Answered By: mgiuca

I’m not a python pro but I believe that unless you can try an alternative for when the parameter doesn’t implement a given method, you shoudn’t prevent exceptions from being thrown. Let the caller handle these exceptions. This way, you would be hidding problems from the developers.

As I have read in Clean Code, if you want to search for an item in a collection, don’t test your parameters with ìssubclass (of a list) but prefer to call getattr(l, "__contains__"). This will give someone who is using your code a chance to pass a parameter that isn’t a list but which has a __contains__ method defined and things should work equally well.

So, I think that you should code in an abstract, generic way, imposing as few restrictions as you can. For that, you’ll have to make the fewest assumptions possible. However, when you face something that you can’t handle, raise an exception and let the programmer know what mistake he made!

Answered By: Pedro Loureiro

If you just want the unimplemented methods to do nothing, you can try something like this, rather than the multi-line try/except construction:

getattr(obj, "sleep", lambda: None)()

However, this isn’t necessarily obvious as a function call, so maybe:

hasattr(obj, "sleep") and obj.sleep()

or if you want to be a little more sure before calling something that it can in fact be called:

hasattr(obj, "sleep") and callable(obj.sleep) and obj.sleep()

This “look-before-you-leap” pattern is generally not the preferred way to do it in Python, but it is perfectly readable and fits on a single line.

Another option of course is to abstract the try/except into a separate function.

Answered By: kindall

If your code requires a particular interface, and the user passes an object without that interface, then nine times out of ten, it’s inappropriate to catch the exception. Most of the time, an AttributeError is not only reasonable but expected when it comes to interface mismatches.

Occasionally, it may be appropriate to catch an AttributeError for one of two reasons. Either you want some aspect of the interface to be optional, or you want to throw a more specific exception, perhaps a package-specific exception subclass. Certainly you should never prevent an exception from being thrown if you haven’t honestly handled the error and any aftermath.

So it seems to me that the answer to this question must be problem- and domain- specific. It’s fundamentally a question of whether using a Cow object instead of a Duck object ought to work. If so, and you handle any necessary interface fudging, then that’s fine. On the other hand, there’s no reason to explicitly check whether someone has passed you a Frog object, unless that will cause a disastrous failure (i.e. something much worse than a stack trace).

That said, it’s always a good idea to document your interface — that’s what docstrings (among other things) are for. When you think about it, it’s much more efficient to throw a general error for most cases and tell users the right way to do it in the docstring, than to try to foresee every possible error a user might make and create a custom error message.

A final caveat — it’s possible that you’re thinking about UI here — I think that’s another story. It’s good to check the input that an end user gives you to make sure it isn’t malicious or horribly malformed, and provide useful feedback instead of a stack trace. But for libraries or things like that, you really have to trust the programmer using your code to use it intelligently and respectfully, and to understand the errors that Python generates.

Answered By: senderle

First of all, the code in your question is not ideal:

try:
    obj.wag_tail()
except AttributeError:
    ...

You don’t know whether the AttributeError is from the lookup of wag_tail or whether it happened inside the function. What you are trying to do is:

try:
    f = getattr(obj, 'wag_tail')
except AttributeError:
    ...
finally:
    f()

Edit: kindall rightly points out that if you are going to check this, you should also check that f is callable.

In general, this is not Pythonic. Just call and let the exception filter down, and the stack trace is informative enough to fix the problem. I think you should ask yourself whether your rethrown exceptions are useful enough to justify all of this error-checking code.

The case of sorting a list is a great example.

  • List sorting is very common,
  • passing unorderable types happens for a significant proportion of those, and
  • throwing AttributeError in that case is very confusing.

If those three criteria apply to your problem (especially the third), I agree with building pretty exception rethrower.

You have to balance with the fact that throwing these pretty errors is going to make your code harder to read, which statistically means more bugs in your code. It’s a question of balancing the pros and the cons.

If you ever need to check for behaviours (like __real__ and __contains__), don’t forget to use the Python abstract base classes found in collections, io, and numbers.

Answered By: Neil G
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.