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..9bf124cef3d 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 an immutable 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."""