Skip to content Skip to sidebar Skip to footer

How To Dynamically Set An Object's Method At Runtime (when The Object Is An Instance Of A Gym Environment)?

I want to override an instance's method. Let's call this instance/object env. The instance method has a method with the signature f(self, other_parameter), which is actually not pu

Solution 1:

I've just noticed that if I try to bind the new method f to envbefore the line gym.envs.register(...), it works, but I would like to change f after the registration.

Meanwhile, I've also noticed that gym can wrap your environment object into another class. In the example above, it turns out that an MyEnv object is created inside an TimeLimit object, which is what env variable in the example above points to. I also notice that env has another property called env, which is the actual object of type MyEnv, so my example above works by only changing the line

env.f = types.MethodType(f, env)

to

env.env.f = types.MethodType(f, env)

This also explains why before registering the environment it works, because env before registering the environment is actually an instance of MyEnv.

Post a Comment for "How To Dynamically Set An Object's Method At Runtime (when The Object Is An Instance Of A Gym Environment)?"