What is the most pythonic and correct way of doing composition aliases?
Here's a hypothetical scenario:
class House:
def cleanup(self, arg1, arg2, kwarg1=False):
# do something
class Person:
def __init__(self, house):
self.house = house
# aliases house.cleanup
# 1.
self.cleanup_house = self.house.cleanup
# 2.
def cleanup_house(self, arg1, arg2, kwarg1=False):
return self.house.cleanup(arg1=arg1, arg2=arg2, kwarg1=kwarg1)
AFAIK with #1 my tested editors understand these just as fine as #2 - auto completion, doc strings etc.
Are there any down-sides to #1 approach? Which way is more correct from python's point of view?
To expand on method #1 unsettable and type hinted variant would be immune to all of the issues pointed out in the comments:
class House:
def cleanup(self, arg1, arg2, kwarg1=False):
"""clean house is nice to live in!"""
pass
class Person:
def __init__(self, house: House):
self._house = house
# aliases
self.cleanup_house = self.house.cleanup
@property
def house(self):
return self._house