# a Python closure function mimics a functor
# a functor can remember initial information
def make_strip(chrs):
"""a functor to strip c in chrs from string mystr"""
def inner(mystr):
return ''.join(c for c in mystr if c not in chrs)
return inner
# characters to strip
chrs = ',;:.?!'
# set up the functor, pass initial data
strip_punctuations = make_strip(chrs)
mystr1 = 'Please, strip punctuations from this string!'
print(strip_punctuations(mystr1))
mystr2 = 'If you are here, you are lost!'
print(strip_punctuations(mystr2))
"""
my result -->
Please strip punctuations from this string
If you are here you are lost
"""
IyBhIFB5dGhvbiBjbG9zdXJlIGZ1bmN0aW9uIG1pbWljcyBhIGZ1bmN0b3IKIyBhIGZ1bmN0b3IgY2FuIHJlbWVtYmVyIGluaXRpYWwgaW5mb3JtYXRpb24KCmRlZiBtYWtlX3N0cmlwKGNocnMpOgogICAgIiIiYSBmdW5jdG9yIHRvIHN0cmlwIGMgaW4gY2hycyBmcm9tIHN0cmluZyBteXN0ciIiIgogICAgZGVmIGlubmVyKG15c3RyKToKICAgICAgICByZXR1cm4gJycuam9pbihjIGZvciBjIGluIG15c3RyIGlmIGMgbm90IGluIGNocnMpCiAgICByZXR1cm4gaW5uZXIKCgojIGNoYXJhY3RlcnMgdG8gc3RyaXAKY2hycyA9ICcsOzouPyEnCgojIHNldCB1cCB0aGUgZnVuY3RvciwgcGFzcyBpbml0aWFsIGRhdGEKc3RyaXBfcHVuY3R1YXRpb25zID0gbWFrZV9zdHJpcChjaHJzKQoKbXlzdHIxID0gJ1BsZWFzZSwgc3RyaXAgcHVuY3R1YXRpb25zIGZyb20gdGhpcyBzdHJpbmchJwpwcmludChzdHJpcF9wdW5jdHVhdGlvbnMobXlzdHIxKSkKCm15c3RyMiA9ICdJZiB5b3UgYXJlIGhlcmUsIHlvdSBhcmUgbG9zdCEnCnByaW50KHN0cmlwX3B1bmN0dWF0aW9ucyhteXN0cjIpKQoKIiIiCm15IHJlc3VsdCAtLT4KUGxlYXNlIHN0cmlwIHB1bmN0dWF0aW9ucyBmcm9tIHRoaXMgc3RyaW5nCklmIHlvdSBhcmUgaGVyZSB5b3UgYXJlIGxvc3QKIiIi