Is there a consensus on how to check a polymorphic instance?

Mike Meng meng.yan at gmail.com
Tue Nov 23 00:59:31 EST 2004


Thank you Dan,
Solution #1 is concluded from 'Text Processing in Python', section
1.1.3, `Pythonic Polymorphisim', around there, here is an example he
provides:

def toDOM(xml_src = None):
from xml.dom import minidom
if hasattr(xml_src, 'documentElement'):
return xml_src     # it's already a DOM object
elif hasattr(xml_src, 'read'):
# it is something that knows how to read data
return minidom.parseString(xml_src.read())
elif type(xml_src) in (StringType, UnicodeType):
# it is a filename of an XML document
xml = open(xml_src).read()
return minidom.parseString(xml)
else:
raise ValueError, "Must be initialized with " + \
"filename, file-like object, or DOM object"
What's your opinion?




More information about the Python-list mailing list