0

While taking outputs from step function of openAI gym environment _ used as the fourth variable.Is it to just for the values which we don't want to use? is there any kind of advantage for using _ instead of any other name?

import gym
env = gym.make('CartPole-v0')
for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        env.render()
        print(observation)
        action = env.action_space.sample()
        observation, reward, done, _ = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break

1 Answers1

1

Using _ for a throw-away variable (one where the code will not use the contents) is self-documenting. Any one who reads the code will immediately see that not using the variable's contents is not an error. (At least, any one who reads the code and is familiar with Python's conventions.)

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50