Using isA and getA in a python way
Fredrik Lundh
fredrik at pythonware.com
Mon Sep 20 08:51:34 EDT 2004
"C Gillespie" wrote:
> I have long list of if statements in the form of:
>
> if obj.isA(): return obj.getA()
> if obj.isB(): return obj.getB()
> if obj.isC(): return obj.getC()
> if obj.isD(): return obj.getD()
> <snip>
>
> Is there a nicer way of doing this, perhap going through the list
> ['A','B','C',..
well, the python way is to do:
return obj.get()
if that's too much work, you could add some kind of identifier
to each obj class, and do:
return getattr(obj, "get" + obj.tag)()
if that's too much work, you could create a map, mapping class names
(or class objects) to tags, and do:
return getattr(obj, "get" + my_lookup_table[obj.__name__])
etc.
</F>
More information about the Python-list
mailing list