Skip to content

Commit

Permalink
Changed .shape to be a property (openai#2397)
Browse files Browse the repository at this point in the history
* Changed .shape to a property, and the internal representation of a shape to ._shape

* Minor docstring fix
  • Loading branch information
RedTachyon authored Sep 11, 2021
1 parent f6742ea commit 7f863fd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gym/spaces/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion gym/spaces/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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."""
Expand Down

0 comments on commit 7f863fd

Please sign in to comment.