<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def remove(self, identified):
 """ Removes an already-created identified object. A key may be passed instead of an identified object. If an object is passed, and its key is held by another object inside the record, an error is triggered. Returns the removed object. """


        by_val = isinstance(identified, Identified)
        if by_val:
            key = identified.key
            if not isinstance(identified, self._class):
                raise self.Error("Such instance could never exist here",
                                 self.Error.INVALID_INSTANCE_CLASS, instance=identified)
        else:
            key = identified

        try:
            popped = self._objects.pop(key)
            if by_val and popped != identified:
                raise self.Error("Trying to pop a different object which also has key '%s'" % popped.key,
                                 self.Error.NOT_SAME_OBJECT, instance=identified, current=popped)
            self._events.remove.trigger(list=self, instance=identified, by_val=by_val)
        except KeyError:
            raise self.Error("No object with key '%s' exists here",
                             self.Error.KEY_NOT_EXISTS, key=key, instance=identified if by_val else None)