Table of Contents

Class PyBuiltins

Namespace
Gym.Utils
Assembly
Gym.dll

Python's built-in conversions and representations, as the Gymnasium source relies on them: float(x) for option parsing, int(x) of a float for pixel coordinates, and repr/str/type for the messages of asserts and errors, which callers and tests match verbatim.

public static class PyBuiltins
Inheritance
PyBuiltins
Inherited Members

Remarks

.NET values stand for the Python values a Gymnasium user would pass: any .NET integer is a Python int, double is float, bool is bool, string is str, null is None. float and Half are NumPy's float32/float16 scalars (Python has no such built-in). A 0-d NDArray is the NumPy scalar of its dtype (np.int64(3)), because NumSharp has no scalar types and Gym.NET returns 0-d arrays where Gymnasium returns NumPy scalars (Discrete.sample()); any other NDArray is an ndarray. A dictionary (IDictionary, or string-keyed pairs such as Info) is a dict, a ITuple a tuple, and a .NET array or other IList a list. Other .NET objects have no Python counterpart: they print through ToString() and their .NET type name.

Numbers print as Python prints them (FloatRepr(double)): the shortest digits that round-trip, positional between 1e-4 and 1e16 and scientific (1e-05, 1e+16) outside, never with .NET's E+16.

Methods

Float(object?)

Python's float(x): a number converted exactly or correctly rounded, a string parsed by Python's rules (whitespace, _ between digits, inf/infinity/nan in any case, overflow to infinity), a 0-d array's element.

public static double Float(object? value)

Parameters

value object

The value, standing for its Python counterpart (see the class remarks).

Returns

double

The float.

Exceptions

ValueError

Thrown for a string that is not a float literal: could not convert string to float: 'abc'.

TypeError

Thrown for null and other non-numbers (float() argument must be a string or a real number, not 'NoneType'), and for an array with dimensions (only 0-dimensional arrays can be converted to Python scalars, NumPy 2.4's message).

OverflowError

Thrown, as Python's OverflowError, for an integer beyond the float range: int too large to convert to float.

FloatRepr(double)

Python's repr(float) (also its str): the shortest digits that round-trip, positional for 1e-4 <= |x| < 1e16 with at least one fractional digit (1.0, 0.0001), scientific otherwise with a signed exponent of at least two digits (1e-05, 1e+16, 1.5e+300); inf, -inf, nan; -0.0.

public static string FloatRepr(double value)

Parameters

value double

The float.

Returns

string

Python's text.

Int(double)

Python's int(x) of a float: truncation toward zero into an unbounded integer. The pixel coordinates of Gymnasium's renderers go through it, so a NaN or infinite coordinate fails here exactly as in Python.

public static BigInteger Int(double value)

Parameters

value double

The float.

Returns

BigInteger

The integer part.

Exceptions

ValueError

Thrown for NaN: cannot convert float NaN to integer.

OverflowError

Thrown, as Python's OverflowError, for an infinity: cannot convert float infinity to integer.

Repr(object?)

Python's repr(x) of the value's Python counterpart (see the class remarks).

public static string Repr(object? value)

Parameters

value object

The value.

Returns

string

2, 0.5, True, None, '1', np.int64(5), array([1]), ...

Str(object?)

Python's str(x) of the value's Python counterpart, what an f-string's {x} prints: a string itself, numbers and None as Repr(object?) prints them, a NumPy scalar without its np.float64(...) wrapper, an array as np.array_str.

public static string Str(object? value)

Parameters

value object

The value.

Returns

string

The text.

StrRepr(string)

Python's repr(str): single quotes unless the text holds a single quote and no double quote, backslash escapes for the quote, \, \t, \n, \r, other control characters as \xhh, and non-printable non-ASCII characters as \xhh, \uhhhh or \Uhhhhhhhh.

public static string StrRepr(string value)

Parameters

value string

The text.

Returns

string

The quoted text.

Remarks

Printability follows .NET's Unicode tables, which can trail Python's by a Unicode version for new characters.

TypeName(object?)

The Python type name of a value's counterpart: int, float, numpy.float32, numpy.ndarray. Messages that quote the bare name (Python's 'int' object is not subscriptable) use it directly.

public static string TypeName(object? value)

Parameters

value object

The value.

Returns

string

The qualified name Python prints inside <class '...'>.

TypeRepr(object?)

Python's str(type(x)): <class 'int'>, <class 'numpy.int64'>, <class 'numpy.ndarray'>.

public static string TypeRepr(object? value)

Parameters

value object

The value.

Returns

string

The type's text.