Type checking in python?

Cliff Crawford cjc26 at nospam.cornell.edu
Wed Jul 19 16:08:31 EDT 2000


* Eric Hopper <hopper at omnifarious.mn.org> menulis:
| 
| > If you insist on type-checking, do it the interface-way:
| > 
| > getattr(object, "required_method") getattr(object, "required_datatype")
| > [etc]
| 
| 	*shudder*

If you don't like doing it by hand, then you can easily write a wrapper
class to do the interface checking for you.  For example:

class Interface:
    def __init__(self, attributes):
        self.attributes = attributes
    def check(self, object):
        for attr in self.attributes:
            if not getattr(object, attr, None):
                return 0
        return 1

FileInterface = Interface(["read", "write", "readline", "readlines", ...])
FileInterface.check(possible_file_object)


-- 
cliff crawford    -><-    http://www.people.cornell.edu/pages/cjc26/
                          Synaesthesia now!            icq 68165166



More information about the Python-list mailing list