Guest User

Untitled

a guest
Oct 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. class Wrap(str):
  2.  
  3. def custom_interface(self):
  4. pass
  5.  
  6. class Wrap(object):
  7.  
  8. def __init__(self, value):
  9. """initiate with a str
  10. """
  11. self._value = value
  12.  
  13. def custom_interface(self):
  14. pass
  15.  
  16. >>> w = Wrap('foo') # wrap any `str`
  17. >>> type(w)
  18. <class 'Wrap'>
  19.  
  20. >>> t = w * 2 # use a `str` operator on `Wrap`
  21. >>> t
  22. 'foofoo'
  23. >>> type(w * 2)
  24. <class 'Wrap'>
  25.  
  26. >>> j = Wrap('.').join(['b', 'a', 'r']) # use `str` interface on `Wrap`
  27. >>> j
  28. 'b.a.r'
  29. >>> type(j)
  30. <class 'Wrap'>
  31.  
  32. >>> j.custom_interface() # and still enjoy dedicated interface
Add Comment
Please, Sign In to add comment