[Python-ideas] [Wild Idea] Static Ducks

Dj Gilcrease digitalxero at gmail.com
Sun Sep 20 00:21:24 CEST 2009


On Sat, Sep 19, 2009 at 3:16 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, 19 Sep 2009 06:19:25 pm Dj Gilcrease wrote:
>> I do not expect this idea to gain much traction but it has been an
>> idea rolling around in my head for a year or two, so I decided to
>> write it all down and see what other thought.
>
> For starters, it's not clear what a static duck is.
>
> Is it a type? Is it a syntax? Is it an extension to existing built-in
> types? All of the above?
>
> When enabled, does the compiler do the type checking at compile time?
> Does it happen at runtime? Both? What sort of type checking is done?
> Pascal-style declarative type checking, or Haskell-style type
> inference?
>
> Why "static duck"? What does this have to do with duck typing?
>
> Rather than give an implementation of how users can create their own
> static ducks, it would be better to give some use-cases for where you
> would use them and what the behaviour is.

The reason I called it static ducks is because you can do "static duck
typing". I didnt list file type objects because that means something
different to different people.

So lets say you want a file like object, any you only care about read,
write, and close.

class FileTypeCheck(StaticDuckBase):
    def _get_value(self):
        return self._value

    def _set_value(self, value):
        if not hasattr(value, 'read') or not hasattr(value, 'write')
or not hasattr(value, 'close'):
            raise TypeError("value must be have read, write and close methods")

        self._value = value

staticducks.add('file', FileTypeCheck)



More information about the Python-ideas mailing list