call to function by text variable
Cameron Laird
claird at lairds.us
Mon Mar 26 11:33:44 EDT 2007
More information about the Python-list mailing list
Mon Mar 26 11:33:44 EDT 2007
- Previous message (by thread): call to function by text variable
- Next message (by thread): call to function by text variable
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
In article <mailman.5612.1174891359.32031.python-list at python.org>, Steve Holden <steve at holdenweb.com> wrote: >Cameron Laird wrote: >> In article <mailman.5604.1174863631.32031.python-list at python.org>, >> Jan Schilleman <jan.schilleman at xs4all.nl> wrote: >>> Hi, >>> >>> try this: >>> func = getattr(operations, ["Replace", "ChangeCase", "Move"][n]) >>> >>> HTH, >>> Jan >>> >>> "ianaré" <ianare at gmail.com> schreef in bericht >>> news:1174862186.134912.117270 at p15g2000hsd.googlegroups.com... >>>> yeah the subject doesn't really make sense does it? >>>> >>>> anyway want I want to do is this: >>>> if n == 1: >>>> >>>> self.operations.insert(pos, operations.Replace.Panel(self, main)) >> . >> . >> . >> I think you meant "...[n - 1]" rather than "...[n]". >> >> I'm a tiny bit surprised no one has organized this in terms >> of a dictionary. I don't know, of course, how robust is the >> characterization of n as a small integer. Maybe >> >> lookup_table = { >> 0: "Replace", >> 1: "ChangeCase", >> 2: "Move"} >> >> captures the sentiment; maybe something else does it better. >> >Surely for this requirement the *only* advantage of a dictionary over a >list is its ability to index with arbitrary values and thereby avoid the >need to use [n-1]. Wouldn't it therefore be less perverse to use > > lookup_table = { > 1: "Replace", > 2: "ChangeCase", > 3: "Move"} > >Of course the dictionary would be a big win if the integer choice values >weren't a linear sequence. Otherwise using a list with a fixed offset is >likely to be quicker. . . . Ugh. Yes. Maybe your question, "Wouldn't it therefore be less perverse ...?", was rhetorical. I feel obliged to answer in public, though, rather than sending the private e-mail I originally wrote, because I want to leave no doubt in the minds of readers of this thread: what I wrote was wrong. Yes, Steve's lookup_table binding (or assignment) was what I had in mind all along, and entirely superior to what I mistakenly wrote; my thanks to him. He also elaborated the correct detail: "if the integer choice values weren't a linear sequence", or if they weren't even integers, or ..., then the dictionary suddenly becomes much more compelling. Incidentally, a fair amount of Python code that looks like if n == 1: ... elif n == 2: ... turns out, in my experience, to have been inherited from some other language in such a way that it turns out n is not even the most natural or expressive determinant. A fully idiomatic rewriting will have a table where the keys are more-humanly-readable strings, rather than small integers.
- Previous message (by thread): call to function by text variable
- Next message (by thread): call to function by text variable
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list