def makefilter(keep):
""" Return a functor that takes a string and returns a copy of that
string consisting of only the characters in 'keep'.
"""
import string
# make a string of all chars, and one of all those NOT in 'keep'
allchars = string.maketrans('', '')
delchars = ''.join([c for c in allchars if c not in keep])
# return the functor
return lambda s,a=allchars,d=delchars: s.translate(a, d)
import string
identifier = makefilter(string.letters + string.digits + '_')
print identifier(string.maketrans('', ''))