Fix RequiredAttrs definition.

The example in python's descriptor tutorial creates a singleton so multiple,
instances share the same attribute. This update fixes the issue based on [1].

[1] http://stackoverflow.com/questions/8718052/where-does-a-python-descriptors-state-go
This commit is contained in:
Tony S Yu
2013-05-28 23:34:52 -05:00
parent d183cce16c
commit 124e38751c
+11 -6
View File
@@ -42,17 +42,22 @@ def start_qtapp():
class RequiredAttr(object):
"""A class attribute that must be set before use."""
def __init__(self, msg):
instances = dict()
def __init__(self, msg='Required attribute not set', init_val=None):
self.instances[self, None] = init_val
self.msg = msg
self.val = None
def __get__(self, obj, objtype):
if self.val is None:
value = self.instances[self, obj]
if value is None:
# Should raise an error but that causes issues with the buildbot.
warnings.warn(self.msg)
return self.val
self.__set__(obj, self.init_val)
return value
def __set__(self, obj, val):
self.val = val
def __set__(self, obj, value):
self.instances[self, obj] = value
class LinearColormap(LinearSegmentedColormap):