Python 27: How to fix/store a value to a method to preserve its interface?
<p>If everything is an object in python then why does the following not work?</p>

<pre><code>class Hello(object):

    def hello(self):
        print(x)

h = Hello()
h.hello.x = "hello world"
</code></pre>

<p>When doing this I get:</p>

<pre><code>AttributeError: 'instancemethod' object has no attribute 'value'
</code></pre>

<p>A way which I can achieve this is by using partial however I am not sure what the effects of that would be on an object. Is there another way to achieve this?</p>

<pre><code>from functools import partial
class Hello(object):

    def hello(self, x):
        print(x)

h = Hello()
newMethod = partial(h.hello, "helloworld")
newMethod()
</code></pre>

<p>Okay, from the comments below, people want a "real" scenario one example which can be considered as the following:</p>

<pre><code>file.txt contains the following list
["ItemID1", "ItemID2", "ItemID3", "ItemID4"]

def BindMethods(self):
    # ["ItemID1", "ItemID2", "ItemID3", "ItemID4"]
    items = readFromFile("file.txt")
    self.bind(AN_EVENT, GenericMethod)

 def GenericMethod(self, i, event):
     # The person calling this method knows that the method
     # only accepts a single argument, not two.
     # I am attempting to preserve the interface which I do not have control over
     data = generateDataFromID(i)
     table[i] = data
</code></pre>

<p>The idea here being that you bind a method to an event whereby the caller is expecting the method of a specific signature i.e. a single input argument.</p>

<p>But at the point of binding you want to pass in some extra information down to the handler. You don't know how much information you want to pass down since it is variable.</p>

<p>A expanded/compile time version of the same thing would be something like this:</p>

<pre><code>file.txt contains the following list
["ItemID1", "ItemID2", "ItemID3", "ItemID4"]

def BindMethods(self):
    # ["ItemID1", "ItemID2", "ItemID3", "ItemID4"]
    items = readFromFile("file.txt")
    self.bind(AN_EVENT, self.Item1Method)
    self.bind(AN_EVENT, self.Item2Method)
    self.bind(AN_EVENT, self.Item3Method)
    ......

 def Item1Method(self, event):
     data = generateDataFromID("Item1")
     table["Item1ID"] = data

 def Item2Method(self, event):
     data = generateDataFromID("Item2")
     table["Item2ID"] = data
</code></pre>

<p><strong>This answer was referring to the question before it was editet; it might not be appropriate anymore.</strong></p>

<p>Actually, there is a solution to this question. In python 3, you can directly assign attributes to bound methods, whereas in python 2.7 it is not that straightforward.</p>

<p>Look at a class definition like this:</p>

<pre><code>class Hello(object):
    def hello(self):
        print(self.hello.x)
</code></pre>

<p>In regular use (instantiate and subsequently call the instance method <code>hello()</code>), the implementation would fail due to the missing attribute <code>x</code>:</p>

<pre><code>h = Hello()
h.hello()

&gt;&gt;&gt; AttributeError: 'function' object has no attribute 'x'
</code></pre>

<p>Changing the code a bit (<strong>Warning:</strong> Hack.) to something like this:</p>

<pre><code>class Hello(object):
    def hello(self):
        if not hasattr(self.hello, 'x'):
            self.hello.__dict__['x'] = 0
        print(self.hello.x)
</code></pre>

<p>Would let you use the class implementation just as usual:</p>

<pre><code>h = Hello()
h.hello()

&gt;&gt;&gt; 0

print(h.hello.x)

&gt;&gt;&gt; 0
</code></pre>

<p>Thus, you <em>can</em> bind attributes to bound methods, but the way you tried it only works in python 3.</p>

<p><strong>Whatever this is good for, you may want to ask yourself whether it is the best way to achieve the desired behavior.</strong></p>

python python-2.7