[Tutor] Dynamic Function Assignment

Greg Lindstrom tubaranger at gmail.com
Fri Oct 21 02:52:12 CEST 2005


Hello-

Suppose I have three objects...

a = MyObject(id=1)
b = MyObject(id=2)
c = MyObject(id=3)

As I read in segments from my file, I need to choose one of the above based
on the segment, something like this;

segment = 'A Segment to Process'

for id, S in (('A',a), ('B',b), ('C',c)):
if segment.upper().startswith(id):
S.DoSomething(segment)

That is, I want to associate an object to work with a condition on the input
segment (there are dozens of segment types and very large files to process).
But I'd like to clean up the "for" statement and have something like this:

for id in ('A', 'B', 'C'):
if segment.upper().startswith(id):
??how can I select the correct object here with a big "if" statement??

The routine works fine as is, but if I can clean up the syntax I will be
able to configure the application via a database; very attractive because I
can process all of the records with one fairly small (~500 line) routine.


Thanks for your consideration...
--greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051020/3d49c075/attachment.htm


More information about the Tutor mailing list