Class Info
The info dict of resets and steps (and the base of Metadata): a string → object map that behaves like
a Python dict. Iteration follows insertion order and re-assigning a key keeps its place. Keys keep
Gymnasium's spelling byte for byte ("TimeLimit.truncated", "episode"). Typed reads go through
Get<T>(IReadOnlyDictionary<string, object?>, string) (info.Get<double>("x_position")), and the built-in envs' infos
convert to typed records (EnvInfo, DR-91).
public class Info : IDictionary<string, object?>, ICollection<KeyValuePair<string, object?>>, IReadOnlyDictionary<string, object?>, IReadOnlyCollection<KeyValuePair<string, object?>>, IEnumerable<KeyValuePair<string, object?>>, IEnumerable
- Inheritance
-
Info
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
A missing key raises KeyError with Python's message ('x'), not
KeyNotFoundException, so code and tests that expect Python's error catch it. Get(string, object?) and
Pop(string, object?) are the defaulting forms, as on a Python dict.
Mutable and not synchronized, like a Python dict. Equals(object), == and
GetHashCode() are by reference, as for any .NET collection (DR-92): a mutable dict hashed by
value would change its hash under a key, and Python refuses to hash a dict at all (unhashable type: 'dict').
Python's ==, which ignores order and compares values as PyEquality does, is
PyEquals(object?).
Constructors
Info()
An empty info, Python's {}.
public Info()
Info(IEnumerable<KeyValuePair<string, object?>>)
An info holding entries in their order, Python's dict(entries): a repeated key keeps
its first position and its last value.
public Info(IEnumerable<KeyValuePair<string, object?>> entries)
Parameters
entriesIEnumerable<KeyValuePair<string, object>>The entries, for example another dictionary.
Exceptions
- ArgumentNullException
Thrown when
entriesor one of its keys is null.
Properties
Count
The number of keys.
public int Count { get; }
Property Value
IsReadOnly
Always false: an info can be edited, like a Python dict.
public bool IsReadOnly { get; }
Property Value
this[string]
Python's info[key]: reading a missing key raises KeyError; assigning adds the key at the end,
or replaces the value of an existing key in place.
public object? this[string key] { get; set; }
Parameters
keystringThe key.
Property Value
- object
The value.
Exceptions
- ArgumentNullException
Thrown when
keyis null.- KeyError
Thrown when reading a key the info does not hold; the message is the key's repr,
'x'.
Keys
The keys, in insertion order (a snapshot; editing the info afterwards does not change it).
public ICollection<string> Keys { get; }
Property Value
Values
The values, in insertion order of their keys (a snapshot).
public ICollection<object?> Values { get; }
Property Value
Methods
Add(string, object?)
Adds a new key, which is what a collection initializer (new Info { { "x", 1 } }) calls. Unlike assigning
through the indexer, an existing key is an error, as for every .NET dictionary.
public void Add(string key, object? value)
Parameters
Exceptions
- ArgumentNullException
Thrown when
keyis null.- ArgumentException
Thrown when the info already holds
key.
Clear()
Python's info.clear(): removes every key.
public void Clear()
Clone()
A shallow copy, the same as Copy(): the legacy Gym.Collections.Dict.Clone, kept so code moving
to Info compiles unchanged (DR-58).
public Info Clone()
Returns
- Info
A new info of the same type with the same entries in the same order. The values are shared, not copied; DeepCopy() copies them.
ContainsKey(string)
Python's key in info.
public bool ContainsKey(string key)
Parameters
keystringThe key.
Returns
Exceptions
- ArgumentNullException
Thrown when
keyis null.
Copy()
Python's info.copy(): a new info of the same type with the same entries in the same order; the values
themselves are shared, as a shallow copy shares them.
public Info Copy()
Returns
- Info
The copy.
CreateEmpty()
Creates an empty instance of this info's own type for Copy() and DeepCopy(), so copying a Metadata yields a Metadata. Subclasses with other constructors override it.
protected virtual Info CreateEmpty()
Returns
- Info
An empty info of the same type.
DeepCopy()
copy.deepcopy(info): a new info of the same type whose values are deep copies (PyCopy), so
editing the copy, even inside a nested dict or array, never shows through the original.
public Info DeepCopy()
Returns
- Info
The copy.
Get(string, object?)
Python's info.get(key, default): the value, or default when the key is missing.
public object? Get(string key, object? @default = null)
Parameters
Returns
- object
The value or the default.
Exceptions
- ArgumentNullException
Thrown when
keyis null.
GetEnumerator()
Enumerates the entries in insertion order.
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<string, object>>
The enumerator; editing the info while enumerating throws, as a Python dict raises
RuntimeError.
Pop(string)
Python's info.pop(key): removes the key and returns its value.
public object? Pop(string key)
Parameters
keystringThe key.
Returns
- object
The removed value.
Exceptions
- ArgumentNullException
Thrown when
keyis null.- KeyError
Thrown when the key is missing; the message is the key's repr.
Pop(string, object?)
Python's info.pop(key, default): removes the key and returns its value, or returns
default when it is missing. step_api_compatibility reads "TimeLimit.truncated"
this way.
public object? Pop(string key, object? @default)
Parameters
Returns
- object
The removed value, or the default.
Exceptions
- ArgumentNullException
Thrown when
keyis null.
PyEquals(object?)
Python's == between dicts (DR-92): the same keys, each with an equal value (AreEqual(object?, object?)),
regardless of insertion order and of the concrete type (an Info equals a Metadata
with the same entries, or a Dictionary<TKey, TValue>, as two dicts compare). == and
Equals(object) compare references instead.
public bool PyEquals(object? other)
Parameters
Returns
Remove(string)
Python's del info[key] without the error: removes the key and closes the gap, so the other keys keep
their relative order.
public bool Remove(string key)
Parameters
keystringThe key.
Returns
Exceptions
- ArgumentNullException
Thrown when
keyis null.
ToString()
Python's repr(info): {'render_modes': ['human', 'rgb_array'], 'render_fps': 50}.
public override string ToString()
Returns
- string
The text.
TryGetValue(string, out object?)
Reads a key without raising when it is missing.
public bool TryGetValue(string key, out object? value)
Parameters
Returns
Exceptions
- ArgumentNullException
Thrown when
keyis null.
Update(IEnumerable<KeyValuePair<string, object?>>)
Python's info.update(other): assigns every entry of other in its order, so new keys go
to the end and existing ones keep their place.
public void Update(IEnumerable<KeyValuePair<string, object?>> other)
Parameters
otherIEnumerable<KeyValuePair<string, object>>The entries to merge in.
Exceptions
- ArgumentNullException
Thrown when
otheror one of its keys is null.