def __init__(self, target, onDelete=None):
        """Return a weak-reference-like instance for a bound method

        target -- the instance-method target for the weak
            reference, must have <im_self> and <im_func> attributes
            and be reconstructable via:
                target.<im_func>.__get__( target.<im_self> )
            which is true of built-in instance methods.
        onDelete -- optional callback which will be called
            when this weak reference ceases to be valid
            (i.e. either the object or the function is garbage
            collected).  Should take a single argument,
            which will be passed a pointer to this object.
        """
        def remove(weak, self=self):
            """Set self.isDead to true when method or instance is destroyed"""
            methods = self.deletionMethods[:]
            del self.deletionMethods[:]
            try:
                del self.__class__._allInstances[ self.key ]
            except KeyError:
                pass
            for function in methods:
                try:
                    if hasattr(function, '__call__' ):
                        function( self )
                except Exception as e:
                    try:
                        traceback.print_exc()
                    except AttributeError:
                        print('''Exception during saferef %s cleanup function %s: %s'''%(
                            self, function, e
                        ))
        self.deletionMethods = [onDelete]
        self.key = self.calculateKey( target )
        self.weakSelf = weakref.ref(getattr(target,im_self), remove)
        self.weakFunc = weakref.ref(getattr(target,im_func), remove)
        self.selfName = getattr(target,im_self).__class__.__name__
        self.funcName = str(getattr(target,im_func).__name__)
Return a weak-reference-like instance for a bound method

        target -- the instance-method target for the weak
            reference, must have <im_self> and <im_func> attributes
            and be reconstructable via:
                target.<im_func>.__get__( target.<im_self> )
            which is true of built-in instance methods.
        onDelete -- optional callback which will be called
            when this weak reference ceases to be valid
            (i.e. either the object or the function is garbage
            collected).  Should take a single argument,
            which will be passed a pointer to this object.
        
stackoverflow/venv/lib/python3.6/site-packages/pydispatch/saferef.py