newbie-ish question on exec statement
Jason Orendorff
jason at jorendorff.com
Tue Mar 12 03:45:01 EST 2002
Gabe Newcomb wrote:
> Specifically, what I want to do is build function
> names dynamically and call them (don't ask why).
>
> I have a feeling that something unexpected (by me anyway)
> is happening with the string I'm trying to pass in as the
> arg to my_function()--this is based on the response I'm
> getting from my function.
Well, it has to do with the number of backslashes you've got.
The following examples should both work:
exec "t = my_" + "function('\\\\\\\\machine\\\\share')"
exec r"t = my_" + r"function(r'\\machine\share')"
But it would be better to do it this way:
fnname = "my_" + "function" # build function name
fn = eval(fnname) # look up the function
fn('\\\\machine\\share') # call it
And there is a better way still, but it depends on why
you're doing what you're doing, and I'm not supposed to ask.
## Jason Orendorff http://www.jorendorff.com/
More information about the Python-list
mailing list