namespaces
Paolino
paolo_veronelli at tiscali.it
Sun Jul 31 15:40:14 EDT 2005
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.
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'
How should I read it? The namespace is half done inside the function?
Thanks Paolino
___________________________________
Yahoo! Messenger: chiamate gratuite in tutto il mondo
http://it.beta.messenger.yahoo.com
More information about the Python-list
mailing list