From: Alan L. <al...@li...> - 2002-06-24 20:31:41
|
Ype, I know rebinding the functions works as you showed -- my actual problem is slightly trickier because I need to apply some renaming algorithms in a rather wholesale way to the method names in a very dynamic fashion (based on how they are being called elsewhere) -- hence it seemed that __getattr__ was the right place to do this kind of monkey business (which, by the way, P/Jython is _very_ good at). I guess I am still in the dark a little as to why my approach didn't work (the docs didn't seem to shed too much light on this dark corner) -- when I can free up some time I'll check out the sources and figure out my misunderstanding. Tnx Alanl ----- Alan Littleford al...@li... <mailto:al...@li...> (650)-255-6909 > -----Original Message----- > From: jyt...@li... > [mailto:jyt...@li...]On Behalf Of Ype Kingma > Sent: Monday, June 24, 2002 12:19 PM > To: jyt...@li... > Subject: RE: [Jython-users] Madness in the method ?? > > > Alan, > > >Marco: > > > > Indeed it does work! I guess there are some subtleties in > Python I've yet > >to appreciate. > > > >Many thanks > >Alan > > > >> > This is probably more of a Python question but here goes: I want to > >> > write a class whose methods with 'self modifying' code in the > >> > following sense: I want to transparently replace calls to a method > >> > foo(...) by a call to foobar(...). > >> > > >> > The following would appear to do the trick: > >> > > >> > class A: > >> > def __getattr__(self, name): > >> > return A.__dict__[name + 'bar'] > >> > > >> > def foobar(self): > >> > print "calling foobar()" > >> > > > > b = A() > b.foo = b.foobar > > or: > > class A: > def foobar(self): print "..." > > def fromNowOnAlsoWithoutBar(self): > self.foo = self.foobar > > > > > try this: > >> > >> class A: > >> def __getattr__(self, name): > >> return getattr(self, name + 'bar') > >> > >> def foobar(self): > >> print "calling foobar()" > >> > > > and everything is fine. > > > > > Have fun, > Ype > > -- > > > ------------------------------------------------------- > Sponsored by: > ThinkGeek at http://www.ThinkGeek.com/ > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.372 / Virus Database: 207 - Release Date: 6/20/2002 > |