[Tutor] Making big 'uns into little 'uns

Peter Otten __peter__ at web.de
Thu Sep 6 16:33:53 CEST 2012


Ray Jones wrote:

> I have a multiple 'if' expression that I need to drastically reduce in
> size, both for readability and to keep errors from creeping in.
> 
> For example, I would like to have the variable 'test' point to the a
> location 'grid[rcount-1][ccount-1]' so that everywhere I would use
> 'grid.....', I could replace it with 'test' How would I accomplish that?

>>> class Grid(object):
...     def __init__(self, rows):
...             self.rows = rows
...     def __getitem__(self, index):
...             return self.rows[index]
...     @property
...     def test(self):
...             return self[-1][-1]
... 
>>> grid = Grid([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])
>>> grid[1]
[4, 5, 6]
>>> grid[1][2]
6
>>> grid.test
12




More information about the Tutor mailing list