Skip to content

Instantly share code, notes, and snippets.

@pzwang
Last active August 29, 2015 14:00
Show Gist options
  • Save pzwang/11390322 to your computer and use it in GitHub Desktop.
Save pzwang/11390322 to your computer and use it in GitHub Desktop.
better-behaved __get__ in bokeh property descriptors
# A replacement for the Instance.__get__() from
# https://github.com/ContinuumIO/bokeh/blob/master/bokeh/properties.py#L832
def __get__(self, obj, owner, type=None):
# If the constructor for Instance() supplied a class name, we should
# instantiate that class here, instead of returning the class as the
# default object
if obj is not None and not hasattr(obj, self._name):
if type and self.default and isinstance(self.default, type):
setattr(obj, self._name, self.default())
elif obj is None:
# Class access; should return something that produces sensible
# help for the help() built-in and IPython. For now, just
# return the descriptor itself, but could also return something
# like self._doc, if self._doc is set in __init__.
return lookup_descriptor(owner, self.name)
else:
return getattr(obj, self._name, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment