Static typing
Shane Hathaway
shane at zope.com
Fri Jul 25 18:39:25 EDT 2003
Bjorn Pettersen wrote:
>>From: Shane Hathaway [mailto:shane at zope.com]
>>def foo(bar, baz):
>> assert isinstance(bar, int)
>> assert isinstance(baz, str)
>
> [...]
>
> ...which means you can't use foo with unicode strings or UserStrings,
That's right, and that's intentional. The function is designed to work
only with byte arrays. This invites someone who wants to pass a unicode
object to first verify that the function will actually work properly
with unicode.
> etc., etc. This is C programming in Python, ugly, inefficient,
> inflexible, and un-Pythonic.
It is highly useful documentation. There is nothing inefficient about
it. It is as flexible as the programmer makes it. It is quite
Pythonic, although you should only declare the types of arguments when
the type matters.
I should say that in actual practice, I haven't used isinstance()
checks. Instead, I've been checking against Zope interfaces.
def foo(conn):
assert IDatabaseConnection.isImplementedBy(conn)
Shane
More information about the Python-list
mailing list