{Spam?} Re: bound vs unbound functions

Terry Reedy tjreedy at udel.edu
Sat Jan 29 20:40:07 EST 2005

"Michael Tobis" <mt at 3planes.com> wrote in message 
news:1107040729.535936.42230 at c13g2000cwb.googlegroups.com...

> I'd like to dynamically add a method to an instance at
> instantiation time.

No can do.  A method is function that is an attribute of a class, even if 
accessed via an instance.  A function added to an instance as an attribute 
of the instance remains a function.  It is instance-specific data that 
methods and other code can use, just like other instance data, for 
instance-specific effects.  Two common examples:

class composer:
  # skip obvious init
  def __call__(self, x): return self.outer(self.inner(x))

sincos = composer(math.sin, math.cos)
# yes, this can also be done with composer function with nested scope

class memoizer: # posted several times
  # skip init again
  def __call__(self,x):
     # return previously computed self.memodict[x] if it exists
     # or calculate, store, and return self.func(x)

\> PS - any idea how to get past google's stupid formatting these days? I
> thought they were supposed to like python, but they just ignore leading
> blanks.

Don't use google to post on clp.  Go to news.gmane.org instead.

Terry J. Reedy

More information about the Python-list mailing list