[Tutor] Line in a file

alan.gauld@bt.com alan.gauld@bt.com
Sun, 21 Jul 2002 23:26:59 +0100


> exceptions are simply generated within code libraries, 

Yes, if you include a function definition as a 'code library'.

> by client code, regardless of who is actually writing it.

There are occasions when you might handle it locally.
A variant of the ubiquitous python idiom of

while 1:
   try:
      # do somerthing
      if exit_condition: raise MyException
      # do more
   except MyException:
       if <we really want to stop>: break

Here we use the exception as a kind of goto before finally 
breaking out of the loop - useful for tidying up open comms 
maybe...

But usually we use raise internally and it gets dealt 
with by the cient.

> > You'd need to create your own file subclass:
> 
> > class RandomFile:

> I see... I don't actually extend the "open()" class at all 

Ideally you would, but the way Python implements files the 
open *function* returns a file *object*. Because open is 
outside the file class it makes it hard to inherit from 
even in 2.2 new classes. So we fall back on delegation 
and making a class that looks like a file but using the 
constructor to call open... Its not really very nice and 
if anyone knows a better way of  'subclassing' files I'd 
love to hear about it. - One of Pythons few wrinkles IMHO...

> is a good illustration of the difference between the "is a" 
> and the "has a" relationship?  :)

An illustration certainly, whether it's good or not....!

Alan G