Chaning instance methods

G. David Kuhlman dkuhlman at netcom.com
Tue Apr 6 20:23:34 EDT 1999


Is this what you are looking for?

---------

# testchangemethod.py:

class Test:
	def m(self):
		pass
	def m1(self):
		print 'I am Test.m1'
	def m2(self):
		print 'I am Test.m2'

---------

>>> from testchangemethod import Test
>>>
>>> t = Test()
>>> Test.m = Test.m1
>>> t.m()
I am Test.m1
>>> Test.m = Test.m2
>>> t.m()
I am Test.m2

--------

I don't understand this too well myself.  However, note that if you
evaluate 'type(Test.m1)' you get something different from what you
get if your evaluate 'type(f)' where f is a function (not a method).

  - Dave


Jody Winston <jody at ldgo.columbia.edu> wrote:
> I don't understand how to change instance methods.  For example:

> class Foo:
> 	def __init__(self):
> 		self.data = 42
> 	def m(self):
> 		print "Foo.m"
> 		print dir(self)

> def m2(self):
> 	print "m2"
> 	print dir(self)

> f = Foo()
> f.m()
> # this fails
> # f.m = m2
> # f.m()
> Foo.m = m2 # Changes all instances of Foo.m
> f.m()

> f2 = Foo()
> f2.m()

> What am I forgetting?

> -- 
> Jody Winston
> Manager SeisRes
> Lamont-Doherty Earth Observatory
> RT 9W, Palisades, NY 10964
> jody at ldeo.columbia.edu, 914 365 8526, Fax 914 359 1631 

> Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
> address may not be added to any commercial mail list with out my
> permission.  Violation of my privacy with advertising or SPAM will
> result in a suit for a MINIMUM of $500 damages/incident, $1500 for
> repeats.






More information about the Python-list mailing list