PDA

View Full Version : Python editiing a variable a user chooses?



jacensolo
July 29th, 2008, 12:49 AM
I want have a class setup with an __init__ functions that assigns attributes to the object which the user chooses. Later, the user may want to change the attributes. I want to have an edit function that asks which attribute to edit. How can I do that without writing out a bunch of if statements?

Simple Ex:


def __init__(self):
self.x = raw_input()
self.y = raw_input()
def edit(self):
h = raw_input("what do you want to change?")
self.(("h?")) = raw_input(h, 'is now...') - I know that raw_input only takes 1 argument.

slavik
July 29th, 2008, 12:53 AM
since I know Perl, I would set up a hash (I think they are called dictionaries in Python) and have the variable be a key. :)

someone with Python knowledge should be able to show you some code to accomplish this.

jacensolo
July 29th, 2008, 02:23 AM
since I know Perl, I would set up a hash (I think they are called dictionaries in Python) and have the variable be a key. :)

someone with Python knowledge should be able to show you some code to accomplish this.

Sounded good. I tried it, but couldn't figure out how to do it with the self in front of it.

Any Python users want to give me some advice? Appreciated.

Torajima
July 29th, 2008, 02:45 AM
Any Python users want to give me some advice? Appreciated.

Try this:


def __init__(self):
self.variables = {}
self.variables['x'] = raw_input()
self.variables['y'] = raw_input()
def edit(self):
h = raw_input("what do you want to change?")
self.variables[h] = raw_input('%s is now...' %h)

days_of_ruin
July 29th, 2008, 03:17 AM
Simple.For every variable you declare just add it to a dictionary

self.vars = {}
somvar = 10
self.vars['somvar'] = somvar

days_of_ruin
July 29th, 2008, 03:20 AM
I want have a class setup with an __init__ functions that assigns attributes to the object which the user chooses. Later, the user may want to change the attributes. I want to have an edit function that asks which attribute to edit. How can I do that without writing out a bunch of if statements?

Simple Ex:


def __init__(self):
self.x = raw_input()
self.y = raw_input()
def edit(self):
h = raw_input("what do you want to change?")
exec("self."+h+" = raw_input(h+' is now...')") - Fixed

That should work too;)If it doesn't its because I posted it wrong:P