namespaces

Rob Williscroft rtw at freenet.co.uk
Sun Jul 31 15:16:46 CEST 2005


Paolino wrote in news:mailman.2453.1122812091.10512.python-
list at python.org in comp.lang.python:

> Rob Williscroft wrote:
> 
> > After 3 or 4 iterations I refactored you code to this:
> >
> > 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 )
> 
> There is a way to access 'all' and 'badcars'  here?
> 
> In my trial translate.all and translate.badcars can be accessed easily 
> and maybe coherently.

I had to do this inorder to access them:

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)

    print "First Call! ", # -- Just to showoff --

    translate.all = all
    translate.badcars = badcars

    return translate( text )


print translate("If I was ...")
print translate("If I was ...")

print "badcars:", len(translate.badcars)

CPython 2.4 on windows XP. But maybe you mean something else ?

> 
> Thanks a lot, and don't tell me 'the dictator has marked the trail' ;)
> 

I got the idea from Hung Jung Lu, here:

http://groups.google.co.uk/group/comp.lang.python/msg/1e63e07c7d83cc7d

How the trail got there I've no idea ;-)

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list