https://rszalski.github.io/magicmethods/
__init__
__repr__
__getattr__
__call__(self, [args...])
context manager
class Closer:
'''A context manager to automatically close an object with a close method
in a with statement.'''
def __init__(self, obj):
self.obj = obj
def __enter__(self):
return self.obj # bound to target
def __exit__(self, exception_type, exception_val, trace):
try:
self.obj.close()
except AttributeError: # obj isn't closable
print 'Not closable.'
return True # exception handled successfully
__copy__(self)
__deepcopy__(self, memodict={})
No comments:
Post a Comment