new.instancemethod questions
schickb
schickb at gmail.com
Thu Jan 29 22:52:09 EST 2009
On Jan 29, 7:38 pm, Mel <mwil... at the-wire.com> wrote:
> schickb wrote:
> > I'd like to add bound functions to instances, and found the
> > instancemethod function in the new module. A few questions:
>
> > 1. Why is instancemethod even needed? Its counter-intuitive (to me at
> > least) that assigning a function to a class results in bound functions
> > its instances, while assigning directly to instances does not create a
> > bound function. So why doesn't assigning a function to an instance
> > attribute result in a function bound to that instance?
>
> If I understand you correctly, rebinding to the instance would break code
> like:
>
> myfakefile.write = sys.stdout.write
>
> where the intent would be to redirect any output through myfakefile straight
> to sys.stdout. The code for the sys.stdout.write function would never find
> the attributes it needed in the instance of myfakefile. To do this,
> methods have to stay bound to their proper instances.
>
1. I'm thinking about assigning free non-bound functions. Like:
class A(object):
pass
def func(self):
print repr(self)
a = A()
a.func = func # Why doesn't this automatically create a bound
function (aka method)?
2. And what is the preferred way to do this if the "new" module and
its instancemethod function are depreciated?
-Brad
More information about the Python-list
mailing list