Detect objects that are touching
Using closest point on mesh

Test code, assumes spheres are named "Sphere[.nnn]" and cubes "Cube[.nnn]"  If a hit from the sphere's centre to a point on a cube is less than the sphere's radius, then it parents them.

import bpy
from bpy import context

scene = context.scene
spheres = [o for o in scene.objects if o.name.startswith("Sphere")]
cubes = [o for o in scene.objects if o.name.startswith("Cube")]

for s in spheres:
    o = s.matrix_world.to_translation()
    radius = sum(s.dimensions) / 6 # sphere radius guestimation
    for cube in cubes:
        cmwi = cube.matrix_world.inverted()
        local_pos = cmwi * o
        (hit, loc, norm, face_index) = cube.closest_point_on_mesh(local_pos)
        if hit:
            v2 = cube.matrix_world * loc   
            # distance from global hit to cube
            l = (v2 - o).length
            if l &lt;= radius:
                cube.parent = s
                cube.matrix_parent_inverse = s.matrix_world.inverted()