Inefficiency of __getattr__

lss at excosoft.se lss at excosoft.se
Wed Oct 4 04:18:03 EDT 2000


If I may...

I think the advantages of static typing best come to show when working
with third-party libraries and APIs. Compare the following method
signatures of an XML DOM API function, in Python and Java:

def appendChild(self, newchild):
   ...

public Node appendChild(Node newChild) {...}

In the Python example, there's no way of easily telling what this
function really wants for its 'newchild' parameter. Sure, anyone can
figure out it's probably a DOM node, but what interface should this DOM
node have in order for the method to work? Can I pass in my own DOM
node implementation and expect it to work? Not unless I either subclass
an existing node implementation from the same API (which can be
dangerous or quite a hassle at times) or actually read the entire
function and check exactly what attributes will need to be accessed
from the node object (a bit more work than most would be willing to
undertake).

In the java example, Node is an interface, org.w3c.dom.Node. This
interface specifies not only the exact interface of the method itself,
but also indirectly the exact interfaces necessary for all objects
passed to and from the function. Thus I don't have to worry about
determining the necessary interfaces for the objects I pass in to the
method, the compiler will do it for me.

In the case of Java interfaces, there is an even more important
advantage: they're standardized. Simply because the org.w3c.dom.Node
interface exists and pretty much all Java DOM-based libraries use it I
can make my own DOM node implementation (which implements this
interface) and make it work with any XPath implementation available,
for example. In Python, I would have to engage in extensive studies of
the XPath library and adapt my DOM node implementation's interfaces for
that particular library, and in the end it probably wouldn't work with
any other available XPath implementations. Providing similar interface
functionality in Python would be pretty easy, but what's the point if
only I use interfaces and they aren't standardized? I think interfaces
could be adapted to work with dynamic typing in Python also, however.
The trick would be to get people to use them and agree on which
interfaces to use.

The basic advantage I see of static typing is: it allows the compiler
to do validation and verification work automatically that otherwise
would have required a lot of time from the programming, especially when
working with 3rd party APIs.

--
Ludvig Svenoniu
Excosoft AB
ludvig at excosoft.se / dominio at hem.passagen.se

In article <NpzC5.1605$l52.81889 at news-west.usenetserver.com>,
  kragen at dnaco.net (Kragen Sitaker) wrote:
> In article <mailman.970553407.740.python-list at python.org>,
>  <jay.krell at cornell.edu> wrote:
> >>Every time I've argued static versus dynamic typing with a
> >>static-typing fan, they've ended up concluding that what they really
> >>hate is not dynamic typing but silent coercions.
> >
> >You haven't argued with me. :)
> >C++ allows silent coercions in a few places that make sense -- cast
to base
> >class, call of user defined "operator T()" and call of user defined
> >constructor, besides the "usual" numeric conversions and conversion
of 0 to
> >any pointer type (this can actually be a problem, like where stuff is
> >defined to operate on integers and pointers, the language would be
much
> >better off with magic like
> >#define NULL __null
> >than #define NULL 0 where "_null" is compiler magic for the null
pointer of
> >any type and 0 favors being an integer before being a pointer..
> >std::vector<int>'s constructor runs into this problem).
> >
> >To keep things from a exploding combinatorially in the compiler, the
> >language only allows one level of silent conversion.
>
> OK, so you agree that silent coercions cause problems in some cases in
> C++, some of which cases are built into the language.
>
> So what's the advantage of static typing over dynamic typing?  Does it
> help you catch bugs, and if so, which ones?
> --
> <kragen at pobox.com>       Kragen Sitaker
<http://www.pobox.com/~kragen/>
> Perilous to all of us are the devices of an art deeper than we
ourselves
> possess.
>                 -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the
Rings"]
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list