I assume you have a parent class for all of those, or at least a mixin. Put a default return function in the parent or mixin, and then override it in those that are different... It's the only proper way of doing it.
Sure, it makes extra code, but at least it's encapsulated, and scalable. Say you wanna add support for five more classes. Instead of altering that code up there, just add the correct code to the new classes. By the looks of it, it's two lines per class (function definition and return line). That's not bad, is it?
If obj
isn't a class which contains a return function, then an exception is raised, which you could catch and ignore with a clean conscience.
class MyMixin:
def my_return(self, *args):
return self.name
... possibly other things...
class SomeClass(MyMixin):
... no alteration to the default ...
class AnotherClass(MyParent, MyMixin):
def my_return(self, *args):
return args[0].normalize(self.identifier)
... blabla
# now, this is in the caller object...
try:
rval = obj.my_return(self) # this is the caller object 'self', not the 'self' in the 'obj'
#dosomething with rval
except Exception:
pass #no rval for this object type, skipping it...