Class PyAttributes
Python's hasattr, getattr and setattr for .NET objects that mirror Python classes: the declared
attributes are the public instance properties and fields (named by PyNameAttribute, or the
snake_case of the C# name: NpRandomSeed is np_random_seed) plus non-public members marked with
PyNameAttribute; objects implementing IDynamicAttributes also hold run-time ones.
public static class PyAttributes
- Inheritance
-
PyAttributes
- Inherited Members
Remarks
Errors are Python's, message for message: a missing attribute raises AttributeError
'CartPoleEnv' object has no attribute 'x'; assigning a read-only property raises
property 'np_random_seed' of 'ExampleEnv' object has no setter. A property whose getter itself raises
AttributeError (a wrapper's _np_random) is absent for HasAttr(object, string), as for Python's
hasattr.
Two deviations follow from static typing: a method is an attribute only when it carries
PyNameAttribute (Python's hasattr(env, "step") is true for every method), and a value must
fit the member's type (Python stores anything): numbers convert between numeric types, anything else that does not
fit raises TypeError.
A PyNameAttribute method (DR-45, get_keys_to_action) reads as the bound method, a delegate to
call. A virtual one is present only once the object's class overrides it, and only when an object with
per-instance presence agrees (Gym.Utils.IPythonAttributePresence), so a base that merely declares it reads as
Python's missing method. As in Python, where an instance's own attributes come before its class's functions, a
run-time attribute of the same name shadows the method, and assigning the name creates that attribute.
Methods
GetAttr(object, string)
Python's getattr(target, name): the declared member's value, else the run-time attribute's.
public static object? GetAttr(object target, string name)
Parameters
Returns
- object
The value (boxed).
Exceptions
- ArgumentNullException
Thrown when
targetornameis null.- AttributeError
Thrown when the object has no such attribute (
'CartPoleEnv' object has no attribute 'x'), or when the member's getter raises it.
HasAttr(object, string)
Python's hasattr(target, name): whether reading the attribute succeeds.
public static bool HasAttr(object target, string name)
Parameters
Returns
- bool
true when GetAttr(object, string) would return a value.
Remarks
Only AttributeError means "absent"; any other exception from a getter propagates, as in Python.
Exceptions
- ArgumentNullException
Thrown when
targetornameis null.
PyTypeName(Type)
The name Python prints for a class: its PyNameAttribute when it has one (a class renamed for a
unique name, DR-24), else the .NET type name without the generic arity (TimeLimit`2 is
TimeLimit); also what Wrapper.class_name() returns.
public static string PyTypeName(Type type)
Parameters
typeTypeThe type.
Returns
- string
The class name.
SetAttr(object, string, object?)
Python's setattr(target, name, value): assigns a declared member, or creates or replaces a run-time
attribute on an object that can hold them.
public static void SetAttr(object target, string name, object? value)
Parameters
targetobjectThe object.
namestringThe Python attribute name.
valueobjectThe value; numbers are converted to a numeric member's type.
Exceptions
- ArgumentNullException
Thrown when
targetornameis null.- AttributeError
Thrown for a read-only property (
property 'np_random_seed' of 'ExampleEnv' object has no setter), for a setter that raises it, and for a new name on an object that cannot hold run-time attributes.- TypeError
Thrown when the value does not fit the member's type.
SnakeCase(string)
The snake_case form of a PascalCase name, the port plan's naming rule read backwards (ยง3.8): NpRandomSeed
is np_random_seed, XThreshold is x_threshold, RGBArray is rgb_array.
public static string SnakeCase(string name)
Parameters
namestringA C# member name.
Returns
- string
The Python attribute name.