fork download
  1. # a Python closure function mimics a functor
  2. # a functor can remember initial information
  3.  
  4. def make_strip(chrs):
  5. """a functor to strip c in chrs from string mystr"""
  6. def inner(mystr):
  7. return ''.join(c for c in mystr if c not in chrs)
  8. return inner
  9.  
  10.  
  11. # characters to strip
  12. chrs = ',;:.?!'
  13.  
  14. # set up the functor, pass initial data
  15. strip_punctuations = make_strip(chrs)
  16.  
  17. mystr1 = 'Please, strip punctuations from this string!'
  18. print(strip_punctuations(mystr1))
  19.  
  20. mystr2 = 'If you are here, you are lost!'
  21. print(strip_punctuations(mystr2))
  22.  
  23. """
  24. my result -->
  25. Please strip punctuations from this string
  26. If you are here you are lost
  27. """
Success #stdin #stdout 0.08s 10840KB
stdin
Standard input is empty
stdout
Please strip punctuations from this string
If you are here you are lost