How to write drivers to be copied and pasted between objects' modifiers? I have assigned a script-expression driver to a field in a modifier.
The expression is a function of properties of the object to which the modifier is assigned, extracted as Single Property variables.


I can CtrlL link the modifier to a new object. I can copy the driver from the old object's modifier, and paste it into the new object's modifier.

But, of course, the driving properties still refer to the old object, and so I have to go in and manually point the variables in the driver to the new object.

How should I use 'self', or suchlike, either in an expression, or in a driver-namespace function, so I can copy the driver between modifiers on separate objects, always referring to the object the modifier is assigned to?

EDIT: 

I've just worked out that self in an expression driving a modifier field, refers to the modifier, not the object the modifier is assigned to. 

AND YET, if I create single-property variables to be used in the expression in the UI, the root is the object. For example, to reach the value of a field in the driven modifier that way, I use modifiers["name"].modifier_property. 

This makes no sense to me at all...
The id_data property of any object returns an ID object

Simple example adding a driver to the count of an array modifier  The modifier is on the default cube.



The driver script, prints out self,  self.id_data the path from self.id_data to self and returns 2.

import bpy

def test(self):
    print(self)
    print(self.id_data)
    print(self.path_from_id())
    return 2

bpy.app.driver_namespace["test"] = test


Here is the output to console

&lt;bpy_struct, ArrayModifier("Array")&gt;
&lt;bpy_struct, Object("Cube")&gt;
modifiers["Array"]


Hence other properties of the modifier can be accessed using self.other_modifier_property and properties of the object it is on via self.id_data.some_object_property

The driver UI deals in ID objects, as does the datapath used by fcurves etc.  You select the (ID) bpy.types.Object instance the modifier is on, and add the datapath from ID object to modifier. In this case self = self.id_data.modifiers["Array"]