A command in a String ?

P

Peter Moscatt

I have a rather odd question:

Is there a way Python can execute a command within a string ?

For example if I had something like:
_str = "fm=OptionMenu(root,xx,'string1','string2','string3')"

And the execute _str.

The reason why I am trying to do this that as I am aware the OptionMenu
hasn't the ability insert options therefore the options would be inserted
by manipulating strings.

Pete
 
F

Fredrik Lundh

Peter said:
I have a rather odd question:

Is there a way Python can execute a command within a string ?

For example if I had something like:
_str = "fm=OptionMenu(root,xx,'string1','string2','string3')"

And the execute _str.

exec _str
The reason why I am trying to do this that as I am aware the OptionMenu
hasn't the ability insert options therefore the options would be inserted
by manipulating strings.

does this do what you want?

strings = "string1", "string2", "string3"
fm = OptionMenu(root, xx, *strings)

</F>
 
T

Tim Daneliuk

Peter said:
I have a rather odd question:

Is there a way Python can execute a command within a string ?

For example if I had something like:
_str = "fm=OptionMenu(root,xx,'string1','string2','string3')"

And the execute _str.

The reason why I am trying to do this that as I am aware the OptionMenu
hasn't the ability insert options therefore the options would be inserted
by manipulating strings.

Pete

Look into the "eval" operation ...
 
P

Peter Moscatt

</F>,

Thanks for the help on this. Yea the 'exec' did the trick.
The second example just placed all the strings in one option button.

Pete
 
F

Fredrik Lundh

Peter said:
Thanks for the help on this. Yea the 'exec' did the trick.
The second example just placed all the strings in one option button.

did you include the asterisk? if you remove that, you get one option. if you
include it, you get three options.

</F>
 
P

Peter Otten

Peter said:
Thanks for the help on this.  Yea the 'exec' did the trick.
The second example just placed all the strings in one option button.

Peter, you most certainly do *not* need an exec statement. If you take the
time to explain what you want to do (as opposed to how you want to do it) a
bit more exhaustively, perhaps with some context code, the profit will be
all on your side.

Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,234
Messages
2,571,179
Members
47,811
Latest member
GregoryHal

Latest Threads

Top