can i implement virtual functions in python ?

Peter Otten __peter__ at web.de
Wed Oct 1 02:39:49 EDT 2003


Andrew Dalke wrote:

 
> I also don't like the double attribute lookup in
> 
>   if not hasattr(obj, "function_name"):
>     ... not implemented ...
>   else:
>     obj.function_name(...)

Have you considered using getattr() with a default?

class Test:
    def there(self):
        print "there"

def default():
    print "default"

t = Test()
for name in "there notThere".split():
    getattr(t, name, default)()


Peter




More information about the Python-list mailing list