Newbie can't figure out documentation practices
Bengt Richter
bokr at oz.net
Fri May 9 20:24:21 EDT 2003
On Fri, 09 May 2003 17:54:55 -0600, Fernando Perez <fperez528 at yahoo.com> wrote:
>Bengt Richter wrote:
>
>
>> I think you may not want to use it in all environments, though:
>>
>> >>> class InterpolationDictClass:
>> ... import sys
>> ... def __getitem__(self, name):
>> ... frame = sys._getframe(1)
>> ... return eval(name, frame.f_globals, frame.f_locals)
>> ...
>> >>> idc = InterpolationDictClass()
>> >>> format =
>> >>> '%(__import__("sys").stdout.write("coulda-been-worse"))s\neh?'
>> >>> print format % idc
>> coulda-been-worseNone
>> eh?
>
>Oh, I know. That evil eval in there is an open door for all sorts of fun
>stuff to happen.
>
>One more reason why (I think) there should be a newbie-friendly way of
>getting my canonical
>
>print '%(self.hello)s' % locals()
>
>to work. Having the cleanest (IMO) solution involve both stack frame
>manipulations and an eval() is not my idea of newbie-friendly ;)
>
See my other post in this thread, but is wrapping your locals() with VS()
close enough? (BTW, I forgot to mention that it's not tested much beyond the
posted examples).
>>> from valsources import ValSources as VS
>>> class Hello:
... hello = 'Hello class variable hello'
... def test(self):
... print '%(self.hello)s' % VS(locals())
...
>>> h = Hello()
>>> h.test()
Hello class variable hello
Regards,
Bengt Richter
More information about the Python-list
mailing list