What static typing makes difficult

David Mertz mertz at gnosis.cx
Sat Oct 25 15:21:20 EDT 2003


In this thread, a number of folks have claimed (falsely) that static
typing never gets in the way.  An example from my own Gnosis Utilities
comes to my mind as a place where I benefit greatly from dynamic
(strong) typing.

The package gnosis.xml.objectify takes an XML source, and turns it into
a "native" Python object.  The function make_instance() can accept a
wide range of different things that might sensibly relate to XML:  a DOM
object, a filename, an XML string, any object with a .read() method.
Just one function deals happily with whatever you throw at it--without
any deep commitments about what type of thing it is (i.e. some novel
file-like object, or some new DOM implementation work without any
problem).  The code is simple (the above function is actually a proxy to
a class):

  class XML_Objectify:
      def __init__(self, xml_src=None, parser=EXPAT):
          self._parser = parser
          if parser==DOM and (hasattr(xml_src,'documentElement'): ...
          elif type(xml_src) in (StringType, UnicodeType):
              if xml_src[0]=='<': ...    # looks like XML
              else:   ...                # looks like filename
          elif hasattr(xml_src,'read'): ...
          else:
              raise ValueError, ...

I would challenge any enthusiast of Haskell, SML, Mozart, Clean, or the
like to come up with something similarly direct.  I know, of course that
the task is *possible*, but I bet you'll need a WHOLE LOT of extra
scaffolding to make something work.

Actually, my strong hunch is that Lisp will also not make things quite
as easy either... but obviously, I know similar ad hoc capability
checking is possible there.

Yours, David...

--
Keeping medicines from the bloodstreams of the sick; food from the bellies
of the hungry; books from the hands of the uneducated; technology from the
underdeveloped; and putting advocates of freedom in prisons.  Intellectual
property is to the 21st century what the slave trade was to the 16th.





More information about the Python-list mailing list