diff --git a/doc/changes.rst b/doc/changes.rst index 4e37bde..830eb3b 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -6,6 +6,9 @@ next * Now handle dict and UserType types as GET/POST params. +* :class:`wsattr` now takes a 'default' parameter that will be returned + instead of 'Unset' if no value has been set. + 0.3b1 ----- diff --git a/wsme/types.py b/wsme/types.py index e35c6c8..91ee70d 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -183,7 +183,7 @@ class wsattr(object): mandatoryvalue = wsattr(int, mandatory=True) """ - def __init__(self, datatype, mandatory=False, name=None): + def __init__(self, datatype, mandatory=False, name=None, default=Unset): #: The attribute name in the parent python class. #: Set by :func:`inspect_class` self.key = None # will be set by class inspection @@ -194,11 +194,14 @@ class wsattr(object): self.datatype = datatype #: True if the attribute is mandatory self.mandatory = mandatory + #: Default value. The attribute will return this instead + #: of :data:`Unset` if no value has been set. + self.default = default def __get__(self, instance, owner): if instance is None: return self - return getattr(instance, '_' + self.key, Unset) + return getattr(instance, '_' + self.key, self.default) def __set__(self, instance, value): try: