On 09/06/2012 10:15 AM, Ray Jones wrote: > On 09/06/2012 07:15 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. >>> >>> >> Easiest way: switch to C++ >> >> There is no preprocessor in Python, and no addresses. There are some >> places you could fake such stuff, but not the expression you have. >> >> If I HAD to do something like this for Python, I'd write a >> preprocessor. But one reason I came to Python is its elegance, and a >> preprocessor isn't elegant. > Well, of all the..... a REAL programming language..... I mean, even > Bash.... ;;)) > > Anyway, it was a shot. 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. -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor