Table of Contents

Class PyKwargs

Namespace
Gym.Utils
Assembly
Gym.dll

Python's keyword arguments for .NET constructors (DR-11): From(object?) turns what a caller passes (a dictionary, an options record, an anonymous object) into a dict of Python names, and Construct(Type, IReadOnlyDictionary<string, object?>) is Python's cls(**kwargs), binding each name to a constructor parameter by its Python name with Python's TypeError texts. make builds envs and wrappers through it.

public static class PyKwargs
Inheritance
PyKwargs
Inherited Members

Remarks

A parameter's Python name is its PyNameAttribute, else the snake_case of its C# name (suttonBartoReward is sutton_barto_reward). Values are converted to the parameter's type where Python's types map onto several .NET ones: any integer (a BigInteger from JSON included) to any numeric type, a float to any floating type, a sequence to an array or list. Python would accept any value and fail later, if at all; here a value that does not fit is a TypeError at the call.

A class can have several public constructors, Python one __init__: the constructors are tried from the one with the most parameters down, and the first that binds every name is used. When none does, the error is the one the constructor with the most parameters raised, the closest counterpart of Python's __init__.

Methods

Construct(Type, IReadOnlyDictionary<string, object?>)

Python's cls(**kwargs): constructs type with each keyword bound to the constructor parameter of that Python name, defaults filling the rest. An open generic type (a wrapper's TimeLimit<,>) is closed over the types its arguments imply: env's Env<TObs, TAct> closes a wrapper over the env's types.

public static object Construct(Type type, IReadOnlyDictionary<string, object?> kwargs)

Parameters

type Type

The class, closed or an open generic definition.

kwargs IReadOnlyDictionary<string, object>

The keyword arguments by Python name.

Returns

object

The new instance.

Remarks

Whatever the constructor throws propagates as it was thrown, not wrapped in a reflection exception.

Exceptions

ArgumentNullException

Thrown when type or kwargs is null.

TypeError

Thrown, with Python's texts, for an unexpected keyword (CartPoleEnv.init() got an unexpected keyword argument 'x') or a missing required one (X.init() missing 1 required positional argument: 'y'); for an abstract class (Can't instantiate abstract class X); and for a value that does not fit its parameter, or generic arguments that cannot be inferred.

OverflowException

Thrown when a number does not fit its parameter's numeric type.

From(object?)

Python's **kwargs of a .NET value, as a new dict of Python names: a dictionary's entries as they are (keys are already Python names), an IGymOptions record's set properties, or any other object's public properties (an anonymous object), named by PyNameAttribute or snake_case. A value that is a string-valued PyEnumAttribute member becomes the string it stands for (RenderMode.Human is "human", DR-84), so a spec records Gymnasium's value.

public static Info From(object? kwargs)

Parameters

kwargs object

The arguments, or null for none.

Returns

Info

A new dict the caller owns; the values are shared, not copied.

Exceptions

TypeError

Thrown for a value that is no mapping (a number, a string, a list): argument after ** must be a mapping, not int, Python's text; or for a dictionary key that is not a string: keywords must be strings.

ArgumentOutOfRangeException

Thrown for a string-valued enum's value it does not declare ((RenderMode)99).

ParameterName(ParameterInfo)

A constructor parameter's Python name: its PyNameAttribute, else the snake_case of its C# name.

public static string ParameterName(ParameterInfo parameter)

Parameters

parameter ParameterInfo

The parameter.

Returns

string

The keyword that binds to it.