This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |