Assigning an empty parent object
Set matrix_parent_inverse
See Does a child object inherit the matrix from the parent?
Key here is that the origin of local space is  calculated from the basis matrix (what you see in the transform UI) and its parent inverse matrix.
There are a number of ways to achieve desired result:

set the empties matrix world to that of the original
and set both the matrix_basis and matrix_parent_inverse of
original to identity. Equivalent to the one case that &quot;didn't move&quot;

By default blender sets the matrix parent inverse, so when parenting is removed the object maintains its prior transform.  Using first method will snap them back to origin.

Set parent matrix world to childs.
Maintain the current location by setting childs matrix_parent_inverse
to its inverted basis matrix.

Test script, method 2.
import bpy

class ObjectTest(bpy.types.Operator):
    &quot;&quot;&quot;Tooltip&quot;&quot;&quot;
    bl_idname = &quot;object.test&quot;
    bl_label = &quot;Array of current object&quot;
    
    def execute(self, context):

        ob = context.active_object

        bpy.ops.object.empty_add()
        mt = context.active_object
        mt.name = f&quot;{ob.name}Array&quot;
        mt.matrix_world = ob.matrix_world
        ob.parent = mt
        ob.matrix_parent_inverse = ob.matrix_basis.inverted()
        return {'FINISHED'}
    
def register():
    bpy.utils.register_class(ObjectTest)


def unregister():
    bpy.utils.unregister_class(ObjectTest)


if __name__ == &quot;__main__&quot;:
    register()

Consider extending this such to selected objects such that can add an empty parent to each, or one parent at active object and all other selected also become children.