On Sun, 31 Jul 2005 21:40:14 +0200, Paolino <[EMAIL PROTECTED]> wrote:
>George Sakkis wrote: > >> Then write a closure. You get both encapsulation and efficience, and as >> a bonus, customization of the translating function: >> >> import string >> >> def translateFactory(validChars=string.letters+string.digits, >> replaceChar='_'): >> all=string.maketrans('','') >> badcars=all.translate(all,validChars) >> table=string.maketrans(badcars, replaceChar*len(badcars)) >> def translate(text): >> return text.translate(table) >> # bind any attributes you want to be accessible >> # translate.badcars = badcars >> # ... >> return translate >> >> >> tr = translateFactory() >> tr("Hel\xfflo") > >This is clean,but I suppose it would get cumbersome if I want to have >more functions in the namespace/factory and it implies all that bindings >in the end. You are allowed to have more than one factory ;-) > >The second point also shows my perplexities about functions namespace: > >def function(): > function.foo='something' > >a=function.foo > >Traceback (most recent call last): > File "<stdin>", line 1, in ? >AttributeError: 'function' object has no attribute 'foo' > You have not yet executed the statement function.foo='something' so you should not expect to see its effect ;-) >How should I read it? The namespace is half done inside the function? > >>> def function(): ... function.foo='something' ... >>> a=function.foo Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'function' object has no attribute 'foo' >>> vars(function) {} Ok, to make the statement execute, execute function: >>> function() >>> a=function.foo >>> a 'something' >>> vars(function) {'foo': 'something'} Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list