[Python-ideas] Interrogate alternate namespace keyword and concept
Steven D'Aprano
steve at pearwood.info
Fri Aug 14 13:17:23 CEST 2009
On Fri, 14 Aug 2009 07:35:55 pm ilya wrote:
> Sorry, it's actually even easier; interrogate() is a one-liner:
>
> class Test:
> '''Class to be interrogated.'''
> def __init__(self, value):
> self.value = value
>
> test = Test(10)
>
> def interrogate(what, how):
> exec(how, what.__dict__)
Apart from the security implications of exec(), it also takes a fairly
hefty performance hit. In Python 2.6:
>>> from timeit import Timer
>>> setup = 'from __main__ import test, interrogate'
>>> Timer("interrogate(test, 'value += 5')", setup).repeat()
[18.503479957580566, 18.218451023101807, 18.218581914901733]
>>> Timer("test.value += 5", setup).repeat()
[0.33056807518005371, 0.33118104934692383, 0.33114814758300781]
That's a factor of 55 times slower -- not precisely an optimization.
--
Steven D'Aprano
More information about the Python-ideas
mailing list