On Thu, 19 May 2005 21:49:46 +0200, Do Re Mi chel La Si Do wrote: > Hi, you, also ! > > A view, with a little difference : > > > def titi(par): > if par>222: > return par*2 > else: > return par*10 > > print titi(123) > print titi(1234) > > #now, change the function, "on instant" > txt="""def titi(par): > if par>222: > return str(par)*2 > else: > return str(par)*5 > """ > exec(txt,globals(),globals()) > > print titi(123) > print titi(1234)
Self-modifying code is almost always a TERRIBLE idea. Using exec is dangerous unless you can control what is being executed. Using exec on code a user supplies is a huge security hole. Self-modifying code is one of those ideas that seems cute when you first learn about it, but in practice is a bad idea. It makes debugging your code much more difficult. It makes comprehending how your code works harder. Generally, you should avoid it, and shun code that modifies itself. Having said that, here is a good example of self-modifying code: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68429 The RingBuffer class dynamically modifies itself once it is full so that its behaviour changes. -- Steven -- http://mail.python.org/mailman/listinfo/python-list