[Gllug] OSS CMSs

Steve Nelson sanelson at gmail.com
Sun May 1 15:30:45 UTC 2005


On 5/1/05, Richard Jones <rich at annexia.org> wrote:

> Imagine the equivalent function in Perl or Python.  This function will
> work fine provided you always pass an object ('s') which has 'empty'
> and 'pop' methods.  If you accidentally call 'drain_stack' with
> something else - an integer, for example, or an object which doesn't
> have either of these methods - then it'll throw a runtime error.

Umm...

assert isInstance(s, Stack)

Seriously, though - to be fair, compile-time type checking won't catch
all your type errors. If you write a library, for example, you have no
way of knowing who's going to call you or what they're going to pass
you. So run-time checking is essential.

In Python, we prefer to ask forgiveness rather than permission.  We
don't check the type of every object before trying to call a method on
it; that generates huge amounts of mostly unnecessary code. We just
catch exceptions:

try:
       s.pop()
       except AttributeError:
              print "Suitable Message"

S.
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list