Bob Rogers wrote:
> So you're saying you don't know the answer? The question wasn't
> "should I use setattr?"
If what you're doing is wrong and backwards then it doesn't matter what
the question is. Best practices are best practices for a reason.
There's no reason to use eval to do what you want
Bob Rogers wrote:
> So you're saying you don't know the answer? The question wasn't
> "should I use setattr?"
>
No, the "*question*" was (paraphrasing slightly) "is [it] possible to
dispense with the compile step and use eval() alone while setting a
property" the *answer* was "you should use se
So you're saying you don't know the answer? The question wasn't
"should I use setattr?"
--
http://mail.python.org/mailman/listinfo/python-list
Why are you using eval in the first place? This isn't bash. Use setattr
and getattr for dynamic attribute access.
--
http://mail.python.org/mailman/listinfo/python-list
Peter Otten wrote:
> Use
>
> eval(s)
>
> to evaluate an expression and
>
> exec s
>
> to execute a statement.
I never thought of using compile() to get around this before. Now I can
finally use print in my lambda functions! Just think of the horrible
code I could write:
f = lambda x: ev
Bob Rogers wrote:
> Given this class:
>
> class C(object):
> def set_x(self, x):
> self._x = x
>
> def get_x(self):
> return self._x
>
> x = property(get_x, set_x)
>
>
> This use of compile() and eval() works as I expected it to:
>
> c = C()
> c.x = 5000
Given this class:
class C(object):
def set_x(self, x):
self._x = x
def get_x(self):
return self._x
x = property(get_x, set_x)
This use of compile() and eval() works as I expected it to:
c = C()
c.x = 5000
n = '\'five thousand\''
code = compile('c.x