call to function by text variable

Discussion in 'Python' started by =?iso-8859-1?B?aWFuYXLp?=, Mar 25, 2007.

  1. 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))

    elif n == 2:

    self.operations.insert(pos, operations.ChangeCase.Panel(self,
    main))

    elif n == 3:

    self.operations.insert(pos, operations.Move.Panel(self, main))

    As you can see all the different functions have the same variables, so
    it would be easier if I could just make a list and use that.

    like this:


    list = ["Replace", "ChangeCase", "Move"]
    textVariable = list[n]
    self.operations.insert(pos, operations.[textVariable].Panel(self,
    main))

    Is something sort of like that possible?


    TIA
     
    =?iso-8859-1?B?aWFuYXLp?=, Mar 25, 2007
    #1
    1. Advertising

  2. ianaré wrote:
    > like this:
    >
    >
    > list = ["Replace", "ChangeCase", "Move"]
    > textVariable = list[n]
    > self.operations.insert(pos, operations.[textVariable].Panel(self,
    > main))
    >
    > Is something sort of like that possible?


    Yes:

    self.operations.insert(
    pos,
    getattr(operations, textVariable).Panel(self.main)
    )
     
    =?ISO-8859-2?Q?Wojciech_Mu=B3a?=, Mar 25, 2007
    #2
    1. Advertising

  3. =?iso-8859-1?B?aWFuYXLp?=

    Steve Holden Guest

    ianaré wrote:
    > 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))
    >
    > elif n == 2:
    >
    > self.operations.insert(pos, operations.ChangeCase.Panel(self,
    > main))
    >
    > elif n == 3:
    >
    > self.operations.insert(pos, operations.Move.Panel(self, main))
    >
    > As you can see all the different functions have the same variables, so
    > it would be easier if I could just make a list and use that.
    >
    > like this:
    >
    >
    > list = ["Replace", "ChangeCase", "Move"]
    > textVariable = list[n]
    > self.operations.insert(pos, operations.[textVariable].Panel(self,
    > main))
    >
    > Is something sort of like that possible?
    >

    Indeed. You don't need to use textual names (though for that you can
    investigate "getattr()) - the following, naturally, is untested:

    ops = [operations.Replace,
    operations.ChangeCase,
    operations.Move
    ]
    self.operations.insert(pos, ops[n-1].Panel(self, main)

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Recent Ramblings http://holdenweb.blogspot.com
     
    Steve Holden, Mar 25, 2007
    #3
  4. =?iso-8859-1?B?aWFuYXLp?=

    Steve Holden Guest

    ianaré wrote:
    > 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))
    >
    > elif n == 2:
    >
    > self.operations.insert(pos, operations.ChangeCase.Panel(self,
    > main))
    >
    > elif n == 3:
    >
    > self.operations.insert(pos, operations.Move.Panel(self, main))
    >
    > As you can see all the different functions have the same variables, so
    > it would be easier if I could just make a list and use that.
    >
    > like this:
    >
    >
    > list = ["Replace", "ChangeCase", "Move"]
    > textVariable = list[n]
    > self.operations.insert(pos, operations.[textVariable].Panel(self,
    > main))
    >
    > Is something sort of like that possible?
    >

    Indeed. You don't need to use textual names (though for that you can
    investigate "getattr()) - the following, naturally, is untested:

    ops = [operations.Replace,
    operations.ChangeCase,
    operations.Move
    ]
    self.operations.insert(pos, ops[n-1].Panel(self, main)

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Recent Ramblings http://holdenweb.blogspot.com
     
    Steve Holden, Mar 25, 2007
    #4
  5. En Sun, 25 Mar 2007 19:36:26 -0300, ianaré <> escribió:

    > list = ["Replace", "ChangeCase", "Move"]
    > textVariable = list[n]
    > self.operations.insert(pos, operations.[textVariable].Panel(self,
    > main))
    >
    > Is something sort of like that possible?


    Try getattr:
    textVariable = "Replace"
    getattr(operations, textVariable) == operations.Replace

    Perhaps you could just store the result in your list; instead of
    ["Replace", "ChangeCase", "Move"], use [operations.Replace,
    operations.ChangeCase, operations.Move] (it's not always applicable, of
    course: maybe operations is reassigned, or those attributes mutate or
    don't always exist...)

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 25, 2007
    #5
  6. =?iso-8859-1?B?aWFuYXLp?=

    Guest

    On Mar 25, 3:36 pm, "ianaré" <> wrote:
    > 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))
    >
    > elif n == 2:
    >
    > self.operations.insert(pos, operations.ChangeCase.Panel(self,
    > main))
    >
    > elif n == 3:
    >
    > self.operations.insert(pos, operations.Move.Panel(self, main))
    >
    > As you can see all the different functions have the same variables, so
    > it would be easier if I could just make a list and use that.
    >



    # Your list would contain the unbound functions:

    unbound_funcs = [operations.Replace.Panel,
    operations.Change.Panel,
    operations.Move.Panel]


    # and invocation would be:

    self.operations.insert(pos, unbound_funcs[n - 1](self, main))


    --
    Hope this helps,
    Steven
     
    , Mar 25, 2007
    #6
  7. Hi,

    try this:
    func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])

    HTH,
    Jan

    "ianaré" <> schreef in bericht
    news:...
    > 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))
    >
    > elif n == 2:
    >
    > self.operations.insert(pos, operations.ChangeCase.Panel(self,
    > main))
    >
    > elif n == 3:
    >
    > self.operations.insert(pos, operations.Move.Panel(self, main))
    >
    > As you can see all the different functions have the same variables, so
    > it would be easier if I could just make a list and use that.
    >
    > like this:
    >
    >
    > list = ["Replace", "ChangeCase", "Move"]
    > textVariable = list[n]
    > self.operations.insert(pos, operations.[textVariable].Panel(self,
    > main))
    >
    > Is something sort of like that possible?
    >
    >
    > TIA
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Jan Schilleman, Mar 25, 2007
    #7
  8. Cool now I can run it through the translator.

    ops = (_("Directory"), _("Replace"), _("ChangeCase"),
    _("Move"), _("Swap"), _("Insert"), _("ChangeLength"))


    self.operations.insert(pos, getattr(operations, ops[n]).Panel(self,
    main))

    Thanks guys!
     
    =?iso-8859-1?B?aWFuYXLp?=, Mar 26, 2007
    #8
  9. On Mar 25, 7:01 pm, "ianaré" <> wrote:
    > Cool now I can run it through the translator.
    >
    > ops = (_("Directory"), _("Replace"), _("ChangeCase"),
    > _("Move"), _("Swap"), _("Insert"), _("ChangeLength"))
    >
    > self.operations.insert(pos, getattr(operations, ops[n]).Panel(self,
    > main))
    >
    > Thanks guys!


    erm ... brainfart LOL. But now I can link it to the translated list.
     
    =?iso-8859-1?B?aWFuYXLp?=, Mar 26, 2007
    #9
  10. In article <>,
    Jan Schilleman <> wrote:
    >Hi,
    >
    >try this:
    >func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])
    >
    >HTH,
    >Jan
    >
    >"ianaré" <> schreef in bericht
    >news:...
    >> 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.
     
    Cameron Laird, Mar 26, 2007
    #10
  11. =?iso-8859-1?B?aWFuYXLp?=

    Steve Holden Guest

    Cameron Laird wrote:
    > In article <>,
    > Jan Schilleman <> wrote:
    >> Hi,
    >>
    >> try this:
    >> func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])
    >>
    >> HTH,
    >> Jan
    >>
    >> "ianaré" <> schreef in bericht
    >> news:...
    >>> 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.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Recent Ramblings http://holdenweb.blogspot.com
     
    Steve Holden, Mar 26, 2007
    #11
  12. In article <>,
    Steve Holden <> wrote:
    >Cameron Laird wrote:
    >> In article <>,
    >> Jan Schilleman <> wrote:
    >>> Hi,
    >>>
    >>> try this:
    >>> func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])
    >>>
    >>> HTH,
    >>> Jan
    >>>
    >>> "ianaré" <> schreef in bericht
    >>> news:...
    >>>> 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.
     
    Cameron Laird, Mar 26, 2007
    #12
  13. =?iso-8859-1?B?aWFuYXLp?=

    Nanjundi Guest

    On Mar 25, 6:36 pm, "ianaré" <> wrote:
    > 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))
    >
    > elif n == 2:
    >
    > self.operations.insert(pos, operations.ChangeCase.Panel(self,
    > main))
    >
    > elif n == 3:
    >
    > self.operations.insert(pos, operations.Move.Panel(self, main))
    >
    > As you can see all the different functions have the same variables, so
    > it would be easier if I could just make a list and use that.
    >
    > like this:
    >
    > list = ["Replace", "ChangeCase", "Move"]
    > textVariable = list[n]
    > self.operations.insert(pos, operations.[textVariable].Panel(self,
    > main))


    try this one:
    textVariable = list[n-1]
    exec( "self.operations.insert(pos, operations.%s.Panel(self, main))" %
    textVariable )

    Not sure if this is an elegant/right way.
    -N
     
    Nanjundi, Mar 26, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. grbgooglefan
    Replies:
    2
    Views:
    439
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    458
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    410
    grbgooglefan
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    266
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    205
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page