From 0592b324d7d7314336f18935d09be2c3c2b3bba5 Mon Sep 17 00:00:00 2001 From: Ariel Kwiatkowski Date: Mon, 6 Sep 2021 10:17:45 +0000 Subject: [PATCH 1/2] Changed .shape to a property, and the internal representation of a shape to ._shape --- gym/spaces/box.py | 2 +- gym/spaces/space.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/gym/spaces/box.py b/gym/spaces/box.py index a7e39d08270..e621cfc35d4 100644 --- a/gym/spaces/box.py +++ b/gym/spaces/box.py @@ -57,7 +57,7 @@ def __init__(self, low, high, shape=None, dtype=np.float32): if np.isscalar(high): high = np.full(shape, high, dtype=dtype) - self.shape = shape + self._shape = shape self.low = low self.high = high diff --git a/gym/spaces/space.py b/gym/spaces/space.py index a392f9bdbab..5faf7a11c70 100644 --- a/gym/spaces/space.py +++ b/gym/spaces/space.py @@ -19,7 +19,7 @@ class Space(object): def __init__(self, shape=None, dtype=None): import numpy as np # takes about 300-400ms to import, so we load lazily - self.shape = None if shape is None else tuple(shape) + self._shape = None if shape is None else tuple(shape) self.dtype = None if dtype is None else np.dtype(dtype) self._np_random = None @@ -33,6 +33,11 @@ def np_random(self): return self._np_random + @property + def shape(self): + """Return the shape of the space as a nonchangeable property""" + return self._shape + def sample(self): """Randomly sample an element of this space. Can be uniform or non-uniform sampling based on boundedness of space.""" From dddc1396afd104ba7ff938d49f9a0f1df766bbb7 Mon Sep 17 00:00:00 2001 From: Ariel Kwiatkowski Date: Mon, 6 Sep 2021 10:26:33 +0000 Subject: [PATCH 2/2] Minor docstring fix --- gym/spaces/space.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gym/spaces/space.py b/gym/spaces/space.py index 5faf7a11c70..9bf124cef3d 100644 --- a/gym/spaces/space.py +++ b/gym/spaces/space.py @@ -35,7 +35,7 @@ def np_random(self): @property def shape(self): - """Return the shape of the space as a nonchangeable property""" + """Return the shape of the space as an immutable property""" return self._shape def sample(self):