dynamicly updating an objects fields

K

kyle.tk

I want to make a function that will work like this:

def updateField(object, fieldName, newValue):
object.fieldName = newValue

fieldName could be anything, the list of objects fields will grow as my
project goes on and i want to reuse the same code without adding more
if statements to it

this is the only way I can see doing it right now:

def updateField(object, fieldName, newValue):
if fieldName = 'name':
nodeDict[nodeID].name = newValue
if fieldName = 'color':
nodeDict[nodeID].color = newValue
..many more if's..

is the top example possible?
 
U

Uwe Lauth

kyle.tk said:
I want to make a function that will work like this:

def updateField(object, fieldName, newValue):
object.fieldName = newValue

This function already exists in python.
It is called settattr.

Regards
Uwe
 
R

Robert Kern

Uwe said:
This function already exists in python.
It is called settattr.

Or rather setattr.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top