Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class classproperty:
- @contract
- def __init__(self, fget: Callable, doc=None):
- self._fget = fget
- if doc is None:
- doc = fget.__doc__
- self.__doc__ = doc
- def __get__(self, instance, cls):
- if cls is None:
- cls = type(instance)
- return self._fget(cls)
- def __set__(self, instance, value):
- raise AttributeError()
- def __delete__(self, instance):
- raise AttributeError()
- class ClasspropertyTest(TestCase):
- class Foo:
- _bar = 'Bar'
- @classproperty
- def bar(cls):
- return cls._bar
- def test(self):
- self.assertEquals(self.Foo.bar, 'Bar')
- def testSet(self):
- with self.assertRaises(AttributeError):
- self.Foo.bar = 'Not bar'
- def testDelete(self):
- with self.assertRaises(AttributeError):
- del self.Foo.bar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement