Sponsored Link •
Bill Venners: I have a theory that 99 percent of the time when I invoke a method in Python it will do what I expect, because in practice it usually does. When I invoke a method in a strongly typed language, 1 percent of the time a bug or misunderstanding may cause the method to do something unexpected. Because even though the contract is clear from a variable's type in a strong typing system, it doesn't mean people don't break the contract. People do break contracts, because of bugs and misunderstandings. I suspect that in both strongly and weakly typed languages, situations in which invoked methods mean something unexpected are equally rare, and for the most part discovered and dealt with during testing.
Guido van Rossum: In Python, you have an argument passed to a
method. You don't know what your argument is. You're assuming that it supports the
readline method, so you call
readline. Now, it could be
that the object doesn't support the
Bill Venners: And then I'll get an exception.
Guido van Rossum: You'll get an exception, which is probably
OK. If this is a mainline piece of code and something could possibly be passed to you
that doesn't have a
readline method, you'll discover that early on during
testing. Just as much as in a typed language when you have an interface and you know
you're getting something that has the right interface but doesn't implement the right thing,
or it throws an unexpected exception. You'll hopefully find that during testing.
In addition in Python, because there aren't fixed protocols, something else can be
passed that also supports
readline and doesn't happen to be a file, but does
exactly what you need. All you need at that point is something that returns lines.
Bill Venners: But it's also possible to pass something that supports
readline that does something I don't expect.
Guido van Rossum: In general in Python, there is a contract, but
the contract is implicit. The contract isn't specified by an interface. There's nothing in
what the parser sees at least that says
x has to be an object that supports
readline that you can call with no arguments and it returns a string that
means a certain thing. But that contract is certainly in the documentation or specification.
In Java, if you say this is something that has a
readline method that
returns a string, what does it mean? Do you expect it to always return the same string?
Does it ever return an empty string? There are all sorts of things that aren't expressed by
that interface that you still have to specify in documentation. That's where the interesting
competition between the different languages exists.