Skip to content

Instantly share code, notes, and snippets.

"""
Example showing that attribute lookups always require consulting the class,
even if an attribute is found in the instance __dict__. This is due to data
descriptors taking prescedence over instance attributes.
"""
class DataDescriptor:
def __get__(self, inst, cls=None):
return 'descriptor'
def __set__(self, inst, value):
pass
class MyContextManager:
def __enter__(self):
return self
def __exit__(self, type_, value, traceback):
# marks exception as being handled, and so it is not re-raised outside of the with block
return True
with MyContextManager():
raise ValueError
data = None