Steven D'Aprano wrote in news:[EMAIL PROTECTED] in comp.lang.python:
> Quoting Rob Williscroft: > >> > def translate( text ) >> > import string all=string.maketrans('','') >> > badcars=all.translate(all,string.letters+string.digits) >> > TABLE = string.maketrans(badcars,'_'*len(badcars)) >> > >> > global translate >> > def translate( text ): >> > return text.translate(TABLE) >> > >> > return translate( text ) > > This is a difficult piece of code to understand and maintain. Its 8 lines, of self contained code. It does everyting its supposed to do and nothing its not. > You have > a function called translate, which in turn calls the string > translate() method. That's okay. But then you have a global variable > *also* called translate -- does that refer to the same function? This is how globals work in python, if you wish to (re)bind to a global before reading it at function scope, you need to say so. > Is > this self-modifying code? That is a dangerous, hard to debug, hard to > understand technique that is too-clever-by-half. > Maybe so, but if true, python as a language is too-clever-by-half. > Then you create a local variable, also called translate, also a No, that isn't how globals work in python, there is no local called translate above. > function, which simply calls the translate method of its argument. A > waste of a function, when all you need to do is call the pre-existing > translate method. If I have understood this code correctly, the > *local* translate is bound to the *global* translate, which is the > function being defined. Close but there isn't, and never was, a *local* translate function. > And lastly, just to complete the confusion, the original function > itself appears to call itself -- presumably the rebound copy that > points to what was the local copy -- recursively, with the same > argument. def f() global g def g(): return "something" return g() f() is a function that (re)creates a global function g() and calls it. Is it just that translate() rebinds itself that is too much, or do you object to f() too ? I do object to f() but only because its useless. > That's a nifty piece of code for showing how clever you are at writing > mildly obfuscated code. But for commercial use, where the code has to > be maintained and debugged, it is a terrible idea. 8 lines of self contained code are a terrible idea !, you have IMO a very strange idea of what is or isn't maintainable. Is using generators and decorators a terrible idea too ? Rob. -- http://www.victim-prime.dsl.pipex.com/ -- http://mail.python.org/mailman/listinfo/python-list