[Tutor] Making big 'uns into little 'uns
Ray Jones
crawlzone at gmail.com
Thu Sep 6 16:59:52 CEST 2012
On 09/06/2012 07:48 AM, Dave Angel wrote:
>>> On 09/06/2012 09:56 AM, 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?
>>>>
>>>> Thanks.
>>>>
>>>>
>>>
> I don't know your use-case. For that matter, I don't even know what
> semantics you mean by the grid[xx][yy] expression. For example, are
> grid, rcount, and ccount globals? Or are you constraining 'test' to
> only be used in the context where they are all visible? Or are you
> defining this location as the offset within grid where rcount and ccount
> happen to point to right now? I can see maybe a dozen "reasonable"
> meanings, each requiring a different sets of constructs in the language
> or its preprocessor.
>
> One thing you can do in Python, but not in any other language I've used,
> is to define a class instance property. For example, if you were
> willing to use q.test instead of test, you could do something like:
>
> class Q(object):
> @property
> def test(self):
> return grid[rcount-1][ccount-1]
>
> That would give you readonly access to an object defined by 3 variables
> that have to be visible to the Q code. And you could make the
> expression more complex if grid is defined elsewhere, for example.
>
> Now once you do q = Q(), you can use
> q.test instead of the larger expression.
>
> Lots of other possibilities in Python. But not with exactly your
> original syntax. Using this one as is would be ugly code, as is your
> original example. So presumably you have an actual use-case where this
> makes sense, other than saving typing.
>
Basically it's as simple as ensuring that an array consists of integers,
and that those integers fall within a certain range. Rather than using
multiple 'if' statements, I was (am, at this point) using multiple tests
within a single 'if' statement. Nothing earth-shatteringly difficult,
but I was simply looking for a way to shorten the overall test
expression with a recursive(? is that the term) variable. No problem though.
Thanks.
Ray
More information about the Tutor
mailing list