[Python-Dev] type categories

Samuele Pedroni Samuele Pedroni" <pedroni@inf.ethz.ch
Sun, 25 Aug 2002 15:51:09 +0200

This is straight from the library (xml.sax.saxutils)
[chosen more because so at least it's real code than because nobody can argue
that it is irrelevant. I find fascinating how much powerful is the argument
"nobody does that often" in discussions about language design]

def prepare_input_source(source, base = ""):
    """This function takes an InputSource and an optional base URL and
    returns a fully resolved InputSource object ready for reading."""

    if type(source) in _StringTypes:
        source = xmlreader.InputSource(source)
    elif hasattr(source, "read"):
        f = source
        source = xmlreader.InputSource()
        if hasattr(f, "name"):

the first problem is the "ontology" problem and how much we want to support
someone who want to strictly check
  (a)  "source has intentionally a file-like read" vs. just
  (b)  "source has some read method"...

This is indipendent of whether we have adapt or declarative interfaces or both.

The above could be written as:

  source = adapt(source,xmlreader.InputSource)

moving the code inside xmlreader.InputSource.__adapt__ .
But does this address (a)?

I would say no.

Then one could simply not implement __adapt__ but leave the burden to the users
to define my-type-with-a-good-read
to xmlreader.InputSource adaptations. Or put code the code inside
and susbitute

     elif hasattr(source, "read"):


   f = adapt(source,???)

so the "ontology" problem is back.

My point is not against adaptation, but that adaptation does not automagically
solve all our problems without further thinking.

I repeat, with both adaptation and interfaces, if one cares about contracts vs.
just signatures, the ontology problem is with us.

Adaptation is probably expressive enough. But the choice between

 - mechanisms to ask and declare whether object implements protocol
 - mechanisms to register adapter factories between protocol A and B
 [I think this is Zope3 model]


 - just adaptation

should be a choice also about convenience, readability, ...
both ways the ontology problem is there.