Python paradigms

Nick Maclaren nmm1 at cus.cam.ac.uk
Sun Apr 9 15:04:30 EDT 2000


In article <8covpn$4jq$1 at slb6.atl.mindspring.net>,
Andrew Dalke <dalke at acm.org> wrote:
>
>I wrote:
>>Nick Maclaren wrote
>>>    x = (a != NULL ? a[i]->weeble : 0) + (b != NULL ? b[i]->wombat : 0)
>
>>How about
>>  x = getattr(a, "weeble", 0) + getattr(b, "wombat", 0)
>
>My code, BTW, is wrong.  I didn't see the "[i]" part of the original
>expression.  To my view of things, this means the expression is
>too complex.

Yes.  That is the point.  The pity is that the expression that I am
trying to evaluate is EXACTLY what the logic of the program requires,
but it cannot be written in Python (or Fortran) without artificial
variables.

>You asked about other idioms.  You could have a class wrapper
>for accessing elements, like:

Again, that works only in a few cases.  When the code is such that
there is not a common pattern to the test condition and evaluated
expression, you have to create a class for every instance.  That
doesn't exactly clarify your code ....


To the several people who suggested:

    (a() and [b()] or [c()])[0]

Thank you for the suggestion.  I have occasionally used perversities
like that (such as '*(x ? &a : &b) = y;' in C to mimic the Algol 68
facility), but I don't regard them as clarifying the code, either!


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QG, England.
Email:  nmm1 at cam.ac.uk
Tel.:  +44 1223 334761    Fax:  +44 1223 334679



More information about the Python-list mailing list