How to add a mirror modifier to all objects of a certain collection?
You're better off using bpy.data.objects to manipulate them. This way you don't need to rely on what is selected, all you need is the objects name.
import bpy

mod_name = 'My-Mirror-Modifier'

for obj in bpy.data.collections[&quot;My-Collection&quot;].all_objects:
    if obj.type == 'MESH':
        mod = obj.modifiers.new(mod_name, 'MIRROR')
        mod.use_axis[0] = False
        mod.use_axis[1] = True
        mod.use_axis[2] = False
        mod.mirror_object = bpy.data.objects[&quot;Light&quot;]