1👍
✅
You could override __getattr__
on the Video
class so that it fetches an attribute of backend
.
def __getattr__(self, attr):
try:
return getattr(self.backend, attr)
except:
raise AttributeError('No attribute named ' + attr)
__getattr__
will only call if the attribute is not found on the Video
object (e.g. it wouldn’t call at all if the user asked for Video.video_id
) so it’s a neat way to extend the exposed attributes of the Video
class. The downside is that it may not be obvious which attributes are available, or it may expose some attributes you don’t wish to. You could get around this by having the VideoBackend
class provide a list of allowed attributes.
def __getattr__(self, attr):
if attr in self.backend.get_allowed_attr():
return getattr(self.backend, attr)
else:
raise AttributeError('No attribute named ' + attr)
👤101
Source:stackexchange.com