Python: Calling a method of an instance's member by name using __getattribute__
<p>I have a class that contains a member object. I would like to call the member's methods as if they were methods of the container class.</p>

<p>The following example illustrates (however in a different context) what I would like to do.</p>

<p>Suppose we have two classes representing two different kinds of products: <strong>Toys</strong> and <strong>Shoes</strong>.
Each class has two methods, let's say, <code>weight()</code> and <code>description()</code>, which return obviusly the item's weight and the item's description.</p>

<p>Now I want to place these products in individual <strong>Boxes</strong> - each box will contain either a single <code>Toy</code> either a single <code>Shoe</code>.</p>

<p>I want to still be able to access the methods <code>weight()</code> and <code>description()</code>, however I want them to be members of the <code>Box</code> instance. Also, I don't want to specifically implement the methods <code>weight()</code> and <code>description()</code> in the <code>Box</code> class, because I don't want to have to implement more methods in the <code>Box</code> class whenever a new method is added to Toy and Shoe.</p>

<p>Here is an example of the "end result" that would make me happy:</p>

<pre><code># Define our products
product1 = Toy('plastic gun', 200)
product2 = Shoe('black leather shoes', 500)

# Now place them in boxes
box1 = Box(product1, box_code='A1234')
box2 = Box(product2, box_code='V4321')

# I want to know the contents of the boxes
box1.description()
box2.description()

# Desired results
&gt;&gt; plastic gun
&gt;&gt; black leather shoes
</code></pre>

<p>My implementation idea is this.</p>

<pre><code>class Product():
    def __init__(self, description, weight):
        self.desc = description
        self.wght = weight

    def description(self):
        return self.desc

    def weight(self):
        return self.weight

class Toy(Product):
    pass

class Shoe(Product):
    pass

class Box(object):
    def __init__(self, product, box_code):
        self.box_code = box_code
        self.product = product

    def __getattribute__(self, name):
        # Any method that is accessed in the Box instance should be replaced by
        # the correspondent method in self.product 
        return self.product.__getattribute__(name)
</code></pre>

<p>However this doesn't work! If I run the "desired" example, shown in the first code block, I get an infinite recursion. So, how is the elegant way of doing this?</p>

<p>Notice that <code>Box</code> has its own parameter <code>box_code</code>, and that in the future I might want to add new methods to <code>Toy</code> and <code>Shoes</code> (for example, a promotional code, or whatever), without having to modify the class <code>Box</code>.</p>

<p>Okay guys, I am eagerly awaiting for your responses.</p>

<hr>

<p><strong>Update</strong>: Answer to the problem
Thanks to Interjay and to Mike Boer for their help.</p>

<p>I just had to substitute the definition of the method <code>__getattribute__</code> in the class <code>Box</code> by <code>__getattr__</code> and it worked perfectly, as suggested by Interjay. The corrected definition of the class Box is:</p>

<pre><code>class Box(object):
    def __init__(self, product, box_code):
        self.box_code = box_code
        self.product = product

    def __getattr__(self, name):
        # Any method that is accessed in the Box instance should be replaced by
        # the correspondent method in self.product 
        return self.product.__getattribute__(name)
</code></pre>

<hr>

<p><strong>Update</strong>: Completing the example using <code>__setattr__</code></p>

<p>After I corrected the problem with the getattr method, I noticed that my classes weren't having the desired behavior when I wanted to set an attribute. For example, if I tried:</p>

<pre><code>box1.desc = 'plastic sword'
</code></pre>

<p>instead of changing the description of product1 (which was incapsulated in box1), a new member called "desc" was created in box1. In order to solve this, I had to define the <code>__setattr__</code> function, as shown below:</p>

<pre><code>def __setattr__(self, name, value):
    setattr(self.product, name, value)
</code></pre>

<p>However, just adding this function creates a recursive call, because in <code>__init__</code> I try to assign <code>self.product = product</code>' which calls the function <code>__setattr__</code>, which does not find the member <code>self.product</code>, so calls the function <code>__setattr__</code> again, recursively ad infinitum.</p>

<p>In order to avoid this problem, I have to change the <code>__init__</code> method in order to assign the new member using the class' dictionary. The complete definition of the class <code>Box</code> is then:</p>

<pre><code>class Box(object):
    def __init__(self, product, box_code):
        self.__dict__['product'] = product
        self.box_code = box_code

    def __getattr__(self, name):
        # Any method that is accessed in the Box instance should be replaced by
        # the correspondent method in self.product 
        return getattr(self.product, name)

    def __setattr__(self, name, value):
        setattr(self.product, name, value)
</code></pre>

<p>Now a funny thing... If I define first the member <code>self.product</code>, as shown above, the program works normally. However, if I try to define first the member <code>self.box_code</code>, the program suffers the recursive loop problem again. Does anybody know why?</p>
