Hacker News new | ask | show | jobs
by sevensor 3018 days ago
Well, calling type() is usually bad, isinstance() is a little better. But if you're expecting a ThisClass and only falling back on strings, as implied by the example, surely this is the classic example of where duck typing is handy:

    try:
        processObject(value)
    except ValueError:
        processString(value)        
Where you've written processObject so that it only works on duck-like objects:

    def processObject(value):
        try:
            value.quack()
        except AttributeError:
            raise ValueError('I was expecting something like a duck!')