469,269 Members | 1,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,269 developers. It's quick & easy.

call to function by text variable

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

Mar 25 '07 #1
12 1158
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)
)
Mar 25 '07 #2
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
Mar 25 '07 #3
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

Mar 25 '07 #4
En Sun, 25 Mar 2007 19:36:26 -0300, ianaré <ia****@gmail.comescribió:
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

Mar 25 '07 #5
On Mar 25, 3:36 pm, "ianaré" <ian...@gmail.comwrote:
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 '07 #6
Hi,

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

HTH,
Jan

"ianaré" <ia****@gmail.comschreef in bericht
news:11**********************@p15g2000hsd.googlegr oups.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))

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


Mar 25 '07 #7
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!

Mar 25 '07 #8
On Mar 25, 7:01 pm, "ianaré" <ian...@gmail.comwrote:
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.

Mar 25 '07 #9
In article <ma***************************************@python. org>,
Jan Schilleman <ja************@xs4all.nlwrote:
>Hi,

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

HTH,
Jan

"ianaré" <ia****@gmail.comschreef in bericht
news:11**********************@p15g2000hsd.googleg roups.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))
Mar 26 '07 #10
Cameron Laird wrote:
In article <ma***************************************@python. org>,
Jan Schilleman <ja************@xs4all.nlwrote:
>Hi,

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

HTH,
Jan

"ianaré" <ia****@gmail.comschreef in bericht
news:11**********************@p15g2000hsd.googleg roups.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.

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

Mar 26 '07 #11
In article <ma***************************************@python. org>,
Steve Holden <st***@holdenweb.comwrote:
>Cameron Laird wrote:
>In article <ma***************************************@python. org>,
Jan Schilleman <ja************@xs4all.nlwrote:
>>Hi,

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

HTH,
Jan

"ianaré" <ia****@gmail.comschreef in bericht
news:11**********************@p15g2000hsd.google groups.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.
Mar 26 '07 #12
On Mar 25, 6:36 pm, "ianaré" <ian...@gmail.comwrote:
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

Mar 26 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Fergus O'Shea | last post: by
4 posts views Thread by Dave | last post: by
3 posts views Thread by John A. Prejean | last post: by
2 posts views Thread by khokimfang | last post: by
4 posts views Thread by hmiller | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.