1
0

empty.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # encoding: utf-8
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. empty.py
  17. from douban code, cool
  18. """
  19. class Empty(object):
  20. def __call__(self, *a, **kw):
  21. return empty
  22. def __nonzero__(self):
  23. return False
  24. def __contains__(self, item):
  25. return False
  26. def __repr__(self):
  27. return '<Empty Object>'
  28. def __str__(self):
  29. return ''
  30. def __eq__(self, v):
  31. return isinstance(v, Empty)
  32. def __getattr__(self, name):
  33. if not name.startswith('__'):
  34. return empty
  35. raise AttributeError(name)
  36. def __len__(self):
  37. return 0
  38. def __getitem__(self, key):
  39. return empty
  40. def __setitem__(self, key, value):
  41. pass
  42. def __delitem__(self, key):
  43. pass
  44. def __iter__(self):
  45. return self
  46. def next(self):
  47. raise StopIteration
  48. empty = Empty()