Python questions from C/Perl/Java programmer
Alex Martelli
alex at magenta.com
Tue Jul 25 07:29:50 EDT 2000
"David Bolen" <db3l at fitlinxx.com> wrote in message
news:uya2q279j.fsf at ctwd0143.fitlinxx.com...
[snip]
> If a function requires that the supplied object support a "write"
> method, then all Python requires is that be the case. And it only
> cares when the function actually tries to make the call, not at
> compile time or function declaration. It doesn't where the object
> fits in a class hierarchy, nor if the object provides all sorts of
> unrelated methods. It only wants that "write" method to be present.
>
> Until you've worked this way you can't imagine how clean, natural and
> "fun" this is - at least IMO. It makes design much cleaner and
> simpler, and means that you can evolve things more simply. No, it's
> not perfect and has its own warts, but I'm sold on it.
Incidentally, this is basically the way that C++'s templates work,
except that they do their work at compile-time so that the constraints
are slightly different -- if the call to method write is inside an if that
never happens to be satisfied, you still need an object with such
a method to instantiate the template, since the compiler is unable
(in general) to ensure the "never happens"; but if it's inside a method
of the template which, itself, is never invoked, then that's OK.
The fact that templates are oblivious to class hierarchies and only
care about a given subset of an object's signature is part of what
gives them their awesome power. Think of Python as further
extending this signature-subset-based paradigm of "typing" to
every use of an object, not just uses happening inside a template,
and (at least if, like me, you're a C++ guru moving to Python:-)
some things may fall into place better...
> Don't get me wrong - if your background is more biased towards heavily
> typed languages (mine was, although I did have some decent experience
> in others) it's sort of a paradigm shift and takes some thinking to
Sort of, I guess. But I come from a lot of very-strong-typing myself, and
I took to Python like a duck to water. Indeed, it's led me to reconsider
much of what I took for granted about strong-typing and its usefulness.
Alex
More information about the Python-list
mailing list