Newbie can't figure out documentation practices

Aahz aahz at pythoncraft.com
Fri May 9 15:05:21 EDT 2003


In article <b9gn4k$dd9$1 at peabody.colorado.edu>,
Fernando Perez  <fperez528 at yahoo.com> wrote:
>
>Ok, time for my yearly rant on interpolation ;)  Can anyone tell me how to
>do cleanly something like (using perl-like syntax to indicate my intent,
>this isn't real code):
>
>print """
>var x = $x
>var y = $y
>var self.z = $self.z
>fun x+y+self.z = $x+$y+$self.z
>var z from other object = $other.z
>z from self plus other = $self.z + $other.z
>"""  % ????
>
>I can't use locals() b/c it won't see the members of self and other. I
>can't update locals() with self/other.__dict__ because they'll clobber
>each other.

There really isn't a good solution to this problem because you're mixing
up multiple namespaces.  You could come up with a decent solution by
using a function call that took a string argument, a default dict
(locals()), plus keyword-named dicts, and then doing your own
interpolation.  I.e.:

print interpolate('''
    var x = $x
    var y = $y
    var self.z = $self.z
    fun x+y+self.z = $x+$y+$self.z
    var z from other object = $other.z
    z from self plus other = $self.z + $other.z
    ''',
    locals(),
    self=self,
    other=other
    )

But I don't think this need is common enough in Python to make it part
of the language definition.  You might look at helping to update PEP 215.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93




More information about the Python-list mailing list