[Python-ideas] [Wild Idea] Static Ducks

Dj Gilcrease digitalxero at gmail.com
Sun Sep 20 07:50:19 CEST 2009

On Sat, Sep 19, 2009 at 10:21 PM, Bruce Leban <bruce at leapyear.org> wrote:
> I don't quite understand what you are trying to solve. Here's a related
> problem: One of the issues with duck typing is that something might look
> sort of like a duck but isn't really and it would be nice to make it easy to
> avoid using an object in a "half-baked" manner:
>     def playWithDuck(d):
>        d.walk()
>        d.quack()
> if someone calls playWithDuck(dog) then the dog's going to get walked before
> trying to make the dog quack fails. I'd like to be able to avoid that. I
> could write:
>    def playWithDuck(d):
>       d.quack   # raises AttributeError if can't quack
>       d.walk()
>       d.quack()
> but of course that doesn't check that d.quack is a function or even has the
> right kind of signature. And I can write
>     def playWithDuck(d):
>       if not isinstance(d, Duck):
>         raise TypeError("need a Duck")
>       ...
> which has it's own problems (I don't need a Duck; I just need something that
> walks like a duck and quacks like a duck).
> Is this the problem you are trying to solve? Or something else?

This is a good description of an issue I would like to solve with this.

More information about the Python-ideas mailing list