lambda abuse? opinions please...

Warren Postma embed at geocities.com
Tue May 9 18:20:33 CEST 2000


This example code shows a trivial database table object which contains a
trivialized version of some objects I'm working on, called the "lightweight
database object". It's intended for places where the BSD Database or DB
Shelve objects are not enough, but you don't need Gadfly's SQL layer. So far
it's about 25k of Python code.

The tables had no lookup functions between them, so I decided to add it. But
before I did that, I decided to do a prototype to "prove the concept" before
going off and hacking my code up into an awful mess.

What I wanted was to be able to generate "lazy lookups". The first time a
simple join was requested, the functional code to implement it would be
"patched in" to the table objects.  The joins themselves use "lazy
indexing", which means the indexes used to create the joins would also be
created on first use, and then kept thereafter. This code is NOT what I have
attached, as it is too complex for posting in newsgroups.

Instead, I have posted a representative sample of the idea, using Lambda to
create a function, in the middle of a query, that will be assigned to a
table instance.  It will then be a function belonging only to a single
instance of a class, instead of the more usual type of function that is part
of all members of a class, and all classes that inherit from it. Is there a
proper name for "instance methods"? (Is that it?)

What's interesting here with Python isn't that I can make one table look up
into another (welcome to the 1970s already) but how I can create an object
class, instantiate it, then muck with it after it's created, to create links
and extend the functionality of the object, at any time I want to. This is
an extremely powerful concept of Python.

It means, right in the middle of running, objects can reorganize themselves
internally to suit the situations they encounter.  They can add and delete
methods and attributes, and even generate entirely new code, and delete it.
I suppose I could even use compile() to go farther than I have gone, but I
think Lambda is enough for now.

At first I mistrusted Lambda, now I really like it. I think it one of the
neatest Magic Beans of Python, so to speak.

What I wonder is, should I use Lambda for this, or could I do this without
benefit of either compile()
or lambda().

Your opinions are much appreciated! ;-)

Warren

attached: lookups.py (2.4 kbytes)



begin 666 lookups.py
M(R!G:79E;B!S;VUE(&EN(&UE;6]R>2!D871A8F%S92!T86)L92!O8FIE8W1S
M+ T*(R!P<F]V:61E(&QO;VMU<"!M971H;V1S(&9R;VT@;VYE('1A8FQE#0HC
M('1O(&%N;W1H97(Z#0H-"G1A8FQE<R ]('M](", at 9VQO8F%L('!L86-E(&9O
M<B!T86)L97,@=&\@;&EV90T*#0IC;&%S<R!T86)L93H-"B @("!D968 at 7U]I
M;FET7U\H<V5L9BQN86UE+&-O;'5M;G,I. at T*(" @(" @("!G;&]B86P@=&%B
M;&5S#0H@(" @(" @('-E;&8N;F%M92 ](&YA;64-"B @(" @(" @<V5L9BYD
M8B ]('M](",@<'ET:&]N(&1I8W1I;VYA<GD at 87,@<'-E=61O+61A=&%B87-E
M#0H@(" @(" @('-E;&8N8V]L=6UN<R ](&-O;'5M;G,-"B @(" @(" @<V5L
M9BYC;VQU;6YS7VP@/2![?0T*(" @(" @("!F;W(@:2!I;B!R86YG92 at Q+&QE
M;BAC;VQU;6YS*2DZ#0H@(" @(" @(" @("!S96QF+F-O;'5M;G-?;%MS96QF
M+F-O;'5M;G-;:5U=(#T@:0T*(" @(" @("!T86)L97-;;F%M95T@/2!S96QF
M#0H@(" @(" @(" @(" -"B @("!D968@;&]O:W5P*'-E;&8L9FEE;&0Q+'9A
M;'5E+&9I96QD,BDZ#0H@(" @(" @(')O=SU.;VYE#0H@(" @(" @(&9O=6YD
M/3 -"B @(" @(" @:V5Y/4YO;F4-"B @(" @(" @:68 at 9FEE;&0Q/3US96QF
M+F-O;'5M;G-;,%TZ(",@:7,@:70@=&AE(&ME>2!Y;W4G<F4@;&]O:VEN9R!U
M<#H-"B @(" @(" @(" @(')O=SUS96QF+F1B6W9A;'5E70T*(" @(" @(" @
M(" @9F]U;F0],0T*(" @(" @("!E;'-E. at T*(" @(" @(" @(" @;B ]('-E
M;&8N8V]L=6UN<U]L6V9I96QD,5T@(R!W:&%T(&EN9&5X(&EN('1H92!R;W<@
M:7,@:70_#0H@(" @(" @(" @("!F;W(@:2!I;B!S96QF+F1B+FME>7,H*3H-
M"B @(" @(" @(" @(" @("!R;W<@/2!S96QF+F1B6VE=#0H@(" @(" @(" @
M(" @(" @:V5Y(#T@:0T*(" @(" @(" @(" @(" @(&EF(')O=UMN72 ]/2!V
M86QU93H-"B @(" @(" @(" @(" @(" @(" @9F]U;F0],0T*(" @(" @(" @
M(" @(" @(" @("!B<F5A:PT*(" @(" @("!I9B!F;W5N9#H-"B @(" @(" @
M(" @(&EF(&9I96QD,CT]<V5L9BYC;VQU;6YS6S!=. at T*(" @(" @(" @(" @
M(" @(')E='5R;B!K97D-"B @(" @(" @(" @(&5L<V4Z#0H@(" @(" @(" @
M(" @(" @<F5T=7)N(')O=UMS96QF+F-O;'5M;G-?;%MF:65L9#)=+3%=#0H@
M(" @(" @(')E='5R;B!.;VYE#0H@(" @(" @(" @(" @(" @#0H@(" @9&5F
M(&%D9')O=RAS96QF+&ME>2QV86QU97,I. at T*(" @(" @("!S96QF+F1B6VME
M>5T@/2!V86QU97,-"B @(" @(" @(" @( T*(" @(&1E9B!D=6UP*'-E;&8L
M8V%L;&UE/4YO;F4I. at T*(" @(" @("!N(#T@<V5L9BYD8BYK97ES*"D-"B @
M(" @(" @;BYS;W)T*"D-"B @(" @(" @9F]R(&D@:6X@;CH-"B @(" @(" @
M(" @(&EF(&-A;&QM92 ]/2!.;VYE. at T*(" @(" @(" @(" @(" @('!R:6YT
M(&DL)RPG*W-T<FEN9RYJ;VEN*&UA<"AR97!R+'-E;&8N9&);:5TI+"<L("<I
M#0H@(" @(" @(" @("!E;'-E. at T*(" @(" @(" @(" @(" @('!R:6YT(&DL
M)RPG*W-T<FEN9RYJ;VEN*&UA<"AR97!R+'-E;&8N9&);:5TI+"<L("<I+"<L
M)RQC86QL;64H<V5L9BQI*0T*#0H-"B,@=&AE<V4@<V%V92!T:&5I<B!S96QF
M('!O:6YT97)S(&EN=&\@=&%B;&5S(&1I8W1I;VYA<GDZ#0H-"G1A8FQE*")N
M86UE<R(L(" @(" @(%LB:60B+")N86UE(B @(%T@(" @("D-"G1A8FQE*")A
M9&1R97-S97,B+" @(%LB:60B+")A9&1R97-S(ET@(" @("D-"G1A8FQE*")P
M:&]N97,B+" @(" @(%LB:60B+")P:&]N92(@(%T@(" @("D-"@T*#0H-"B,@
M861D('-O;64@<F]W<SH-"@T*=&%B;&5S6R)N86UE<R)=+F%D9')O=R@@,2Q;
M)VUA9V=Y)UT@*2 C(&ME>2P at 6W9A;'5E<UT-"G1A8FQE<ULB;F%M97,B72YA
M9&1R;W<H(#(L6R=B<FEA;B==("D@(R!K97DL(%MV86QU97-=#0H-"G1A8FQE
M<ULB<&AO;F5S(ETN861D<F]W*#$L6R<U-34M-34U+34U-34G72D-"G1A8FQE
M<ULB<&AO;F5S(ETN861D<F]W*#(L6R<W-S<M-S<W+3<W-S<G72D-"@T*#0HC
M('!R;W9I9&4 at 82!L:6YK(&9R;VT@=&AE(&YA;65S(&]B:F5C="!T;R!T:&4@
M<&AO;F5S(&]B:F5C= T*=&%B;&4@/2!T86)L97-;(FYA;65S(ET-"G1A8FQE
M+G!H;VYE(#T@=&%B;&5S6R)P:&]N97,B70T*#0HC(&=E;F5R871E(&$@9'EN
M86UI8R!F=6YC=&EO;B!U<VEN9R!L86UB9&$Z#0IT86)L92YG971P:&]N92 ]
M(&QA;6)D82!S96QF+&ED.B!S96QF+G!H;VYE+FQO;VMU<"@@)VED)RQI9"PG
M<&AO;F4G*0T*#0H-"B,@=')Y(&ET(&]U=#H-"@T*<')I;G0@(G1A8FQE("=N
M86UE<R<Z(@T*=&%B;&4N9'5M<"@I(", at 9'5M<"!T86)L92!C;VYT96YT<RP@
M=VET:&]U="!L;V]K=7!S#0IP<FEN=" B("(-"G!R:6YT(")T86)L92 G;F%M
M97,G("L@;&]O:W5P('!H;VYE(&YU;6)E<CHB#0IT86)L92YD=6UP*'1A8FQE
E+F=E='!H;VYE*0T*<')I;G0@(B B#0IP<FEN=" B96YD+B(-"@``
`
end




More information about the Python-list mailing list