define
Bengt Richter
bokr at oz.net
Fri May 9 13:26:22 EDT 2003
On Fri, 09 May 2003 12:22:15 GMT, Alex Martelli <aleax at aleax.it> wrote:
><posted & mailed>
>
>Turhan Ozen wrote:
>
>> I have a long list of parameters. I want to keep them in an array in
>> order to pass them to functions. But I want to use the name of each
>> parameter in the formulas to make them easy to read. I can use x[index]
>> in the formulas. I am trying to have another name for each x[index]. If
>> I do anything to this secondary reference, the same is done to the
>> corresponding x[index].
>>
>> It is possible to do this in C and it looks more readable.
>
>It looks to me that a good solution is a "list with named items" class.
>E.g., in Python 2.3 (warning, untested code):
>
[... the code ...]
>and then you can access and/or set e.g. x[1] equivalently to
>x.another. Why you would want to use indexed access as well as
>named access, I don't know, but surely it's MUCH clearer and more
>readable if any access on an item of x does use x.something rather
>than just a bare unqualified 'something' as the name!
>
I can sympathize with a desire to avoid writing, e.g.,
x.y = (-x.b + x.sign*sqrt(x.b**2 - 4.0*x.a*x.c))/2.0*x.a
in favor of something like, e.g.,
using y, b, sign, a, c with x:
y = (-b + sign*sqrt(b**2 - 4.0*a*c))/2.0*a
Or, perhaps, alternatively,
with (y, b, sign, a, c) in x:
y = (-b + sign*sqrt(b**2 - 4.0*a*c))/2.0*a
Or, when it applies to every symbol used (same-line suite optional, of course),
with x: y = (-b + sign*sqrt(b**2 - 4.0*a*c))/2.0*a
Perhaps it's time to reexamine whether it could be done nicely somehow?
Regards,
Bengt Richter
More information about the Python-list
mailing list