If it's just the one method, then what you've got is honestly not that bad. It quite straightforwardly says that baz is part of the interface of Boo, and it achieves this by delegating to the contained Foo instance.
A slightly shorter version would be to use a property:
class Foo (object) :
def baz (self) :
return 324
class Bar (object) :
def __init__ (self) :
self.foo = Foo()
@property
def baz(self):
return self.foo.baz
def __getattr__(self, attr):
return getattr(self.foo, attr)
bar = Bar()
print bar.foo.baz()
print bar.baz()
There's still an extra function call though, and it's perhaps slightly less obvious that baz is a method.
Without resorting to rewriting your inheritance tree, I'd just go with what you have if baz is the only method that needs to be delegated to the contained instance like that. If you want to delegate many methods, then there are some options:
If you want all methods of Foo to be callable from the Bar instance that contains a Foo, then you could try using __getattr__:
class Foo (object) :
def baz (self) :
return 324
class Bar (object) :
def __init__ (self) :
self.foo = Foo()
def __getattr__(self, attr):
return getattr(self.foo, attr)
bar = Bar()
print bar.foo.baz()
print bar.baz()
But this has some obvious drawbacks.
- It's not clear what methods
Bar actually provides, either by reading the source code or by introspection.
- All of
Foo's methods and attributes (that aren't provided directly by Bar) will be exposed through Bar's interface, which may be undesirable.
Of course, you could put more logic in the __getattr__ method to only delegate certain things, not everything.
I tend to go with this sort of approach when all a Bar is is a simple wrapper around a Foo, because then "a Bar behaves mostly like a Foo" is part of the conceptual interface (rather than Foo being an internal implementation detail), and it reduces Bar's definition to describing the ways it is not like a Foo.
Another approach is to use Python's dynamic nature to define properties on Bar that delegate to Foo without having to handwrite them all. Something like this would do the trick:
class Foo (object) :
def baz (self) :
return 324
class Bar (object) :
def __init__ (self) :
self.foo = Foo()
for _method in ['baz', 'blob']:
@property
def _tmp_func(self, _name=_method):
return getattr(self.foo, _name)
locals()[_method] = _tmp_func
# del to avoid name pollution
del _method, _tmp_func
bar = Bar()
print bar.foo.baz()
print bar.baz()
The advantages of this approach over the __getattr__ one are that you've got an explicit list of methods that are redirected when you're reading the code, and they'll all "look like" normal properties of Bar at runtime because they are normal properties. It's obviously a lot more code than your original if you only need this for one or two methods though! And it's a little subtle (the need to pass _method into _tmp_func via a default argument because of the way Python's closures work isn't necessarily obvious). But it would be relatively easy to hide package up the logic for doing this in a class decorator, which would gain you the ability to create more classes that delegate the implementation of some of their methods to one of their attributes.
There are many variations on these sorts of things you can come up with.
bazis just a utility method and has no state in relation toBarthen you don't need a class. You just need functions