object visibility | renderability

Hi

I´m new to scripting.
I tried to write a script to animate the objects visibility/ renderability on one layer.
I managed to change it for ONE activated object.
How can I change it for all objects on the layer?
I can select them all by using:
bpy.ops.object.select_by_layer(extend=False, layers=1)
but then only the last one is set active.
Do I have to write a range?(but I do not know how to do that)
I know there is the trick to use only one active layer and move the objects to that layer at a particular frame. But the file has many objects.
It would be nice if you can help me, here is the script (don´t laugh, it´s very small;):
import bpy

def restrict_render_test():
ob = bpy.context.active_object
scene = bpy.context.scene
scene.frame_set(1)
ob.hide= True
ob.hide_render=True
ob.keyframe_insert(data_path= “hide”)
ob.keyframe_insert(data_path= “hide_render”)
scene.frame_set(10)
ob.hide= False
ob.hide_render=False
ob.keyframe_insert(“hide”)
ob.keyframe_insert(“hide_render”)

restrict_render_test()

or is it easier to work with:
bpy.ops.outliner.visibility_toggle()
but I do not know how to use it.

what is the best way?

how about using a loop?

def hide_meshes(hide):
    for ob in bpy.context.selected_objects:
        if ob.type != 'MESH':
            continue
        ob.hide = hide
        ob.hide_render = hide
        ob.keyframe_insert(data_path="hide")
        ob.keyframe_insert(data_path="hide_render")
        
bpy.ops.object.select_by_layer(extend=False, layers=1)

scene = bpy.context.scene

scene.frame_set(1)
hide_meshes(True)

scene.frame_set(10)
hide_meshes(False)

Thanks!

It works!
But do you know a way similar to that to also change the visibility/renderability of empties?
I tried to add them in the "if ob.type != ´MESHor ÈMPTY "but that did not work.

Thanks!

It worked.
I tried to include the empties too.if ob.type!= ´MESH`or ´EMPTY´
but that did not work. Guess the ! and or are mutually exclusive?

you can do like:

if ob.type not in (‘MESH’, ‘EMPTY’):