Cliff Crawford cjc26 at
Thu Feb 1 21:18:07 EST 2001

* drewpc at <drewpc at> menulis:
| Now, my question.  Is this something that people would be interested in
| seeing in Python?  If so, then I think I'll write up a PEP (Python
| Enhancement Proposal, about this.
| Comments?  Cheap shots?  One-liners?

Well, the usual way to indicate that a class implements, say, the file
interface, is to write a comment saying "this class implements the file
interface". ;)  You probably don't need anything more complicated than
that, since Python is dynamically typed, blah blah, etc. (where "blah
blah, etc." is meant to sum up the many discussions on about this
very topic ;)  If you were really paranoid, you could always do
something like this:

def func(f):
    if not hasattr(f, "readlines"):
        raise NeedFileObjectError
    for line in f.readlines():
        # rest of function goes here

except NeedFileObjectError:
    print >>sys.stderr, "You must use a file object!"

Of course, most people would just leave out the hasattr() check, and let
func() raise an AttributeError if it's passed something which doesn't
act like a file object.

    either-<wink>-ly y'rs, Cliff

Cliff Crawford     
                             print "Just another Python hacker"

More information about the Python-list mailing list