Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. class classproperty:
  2.     @contract
  3.     def __init__(self, fget: Callable, doc=None):
  4.         self._fget = fget
  5.         if doc is None:
  6.             doc = fget.__doc__
  7.         self.__doc__ = doc
  8.  
  9.     def __get__(self, instance, cls):
  10.         if cls is None:
  11.             cls = type(instance)
  12.         return self._fget(cls)
  13.  
  14.     def __set__(self, instance, value):
  15.         raise AttributeError()
  16.  
  17.     def __delete__(self, instance):
  18.         raise AttributeError()
  19.  
  20. class ClasspropertyTest(TestCase):
  21.     class Foo:
  22.         _bar = 'Bar'
  23.  
  24.         @classproperty
  25.         def bar(cls):
  26.             return cls._bar
  27.  
  28.     def test(self):
  29.         self.assertEquals(self.Foo.bar, 'Bar')
  30.  
  31.     def testSet(self):
  32.         with self.assertRaises(AttributeError):
  33.             self.Foo.bar = 'Not bar'
  34.  
  35.     def testDelete(self):
  36.         with self.assertRaises(AttributeError):
  37.             del self.Foo.bar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement