views:

75

answers:

2

Hi everyone!
I was wondering, is there a Python equivalent to Apache commons' PropertyUtilsBean?

Edit:
For example, I'd like to be able to make this assignment

x.y[2].z = v

given "y[2].z" as a string.
Please note, I'm asking just because I'd like to not reinvent the wheel :)

+1  A: 

Do you mean something like setattr?

From its docstring:

setattr(object, name, value)

Set a named attribute on an object; setattr(x, 'y', v) is equivalent to ``x.y = v''.

Roberto Liffredo
Well yes I mean something like that, but more complex.. For example x.y[2].z = v :)
Joril
In that case, I think you need to use exec, as per Vinay's suggestion - even if I usually find exec usage quite a bad hack, I do not see any other possible easy solution.
Roberto Liffredo
+1  A: 

Why do you need such a thing when there's exec?

Vinay Sajip
*Snap!* Didn't think of exec.. Damn Java legacy :D Thanks!
Joril
but remember exec("os.system('sudo rm -rf /')"), so you should be a little careful with exec :). It's probably best to stick with setattr/getattr when you can get away with it.
Aaron Watters
by the way, don't try the above...
Aaron Watters