Skip to content

Commit 3f75fc4

Browse files
authored
cell space: Add convenience properties for grid width and height (#2348)
90% of all use cases of grid will be 2D so it is convenient to have easy access to the width and the height of the grid. While porting the examples over to the experimental grid spaces, I frequently need to write `self.grid.dimensions[0]` etc. This makes it a bit more readable because I can just do `self.grid.width` etc.
1 parent 48dc35f commit 3f75fc4

File tree

1 file changed

+13
-0
lines changed
  • mesa/experimental/cell_space

1 file changed

+13
-0
lines changed

mesa/experimental/cell_space/grid.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@ class Grid(DiscreteSpace[T], Generic[T]):
2222
random (Random): the random number generator
2323
_try_random (bool): whether to get empty cell be repeatedly trying random cell
2424
25+
Notes:
26+
width and height are accessible via properties, higher dimensions can be retrieved via dimensions
27+
2528
"""
2629

30+
@property
31+
def width(self) -> int:
32+
"""Convenience access to the width of the grid."""
33+
return self.dimensions[0]
34+
35+
@property
36+
def height(self) -> int:
37+
"""Convenience access to the height of the grid."""
38+
return self.dimensions[1]
39+
2740
def __init__(
2841
self,
2942
dimensions: Sequence[int],

0 commit comments

Comments
 (0)