Public paste
Undefined
By: Guest | Date: Oct 27 2015 15:15 | Format: Python | Expires: never | Size: 528 B | Hits: 362

  1. class iget(object):
  2.     """A descriptor to make the creation of named tuples easier."""
  3.     def __init__(self, i):
  4.         """Store the index of the tuple's item to access."""
  5.         self.i = i
  6.  
  7.     def __get__(self, instance, owner=None):
  8.         """Access the tuple item that I am about."""
  9.         return instance[self.i]
  10.  
  11.  
  12. class example(tuple):
  13.     spam = iget(0)
  14.     eggs = iget(1)
  15.     ham = iget(2)
  16.  
  17.  
  18. x = example('foo', 'bar', 'bas')
  19. assert x.spam == 'foo'
  20. assert x.eggs == 'bar'
  21. assert x.ham == 'bas'