Newbie can't figure out documentation practices

jcollins jcollins at boulder.net
Fri May 9 14:07:07 EDT 2003


sismex01 at hebmex.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
>>"""  % ????
>>
>>    
>>
>
>Hmmm... I'd first:
>
>  
>
>>>>def D(**args): return args
>>>>        
>>>>
>
>and then:
>
>print """
>var x = %(x)s
>var y = %(y)s
>var self.z = %(self_z)s
>fun x+y+self.z = %(x)s+%(y)s+%(self_z)s
>var z from other object = %(other_z)s
>z from self plus other = %(self_z)s + %(other_z)s
>""" % D(x=..., y=..., self_z=self.z, other_z=other.z)
>
>But, I think, I suspect that you don't like %(...)s
>  
>
Warning: untested:

class evaldict:
    def __init__(self, dict):
        self.mydict = dict
    def __getitem__(self, key):
        return eval(key, self.mydict)

print """
var x = %(x)s
var y = %(y)s
var self.z = %(self.z)s
fun x+y+self.z = %(x)s+%(y)s+%(self.z)s
var z from other object = %(other.z)s
z from self plus other = %(self.z)s + %(other.z)s""" % evaldict(locals())

Easily modified to include globals...

>  
>
>>I've been told to write the above with tons of print 
>>statements (one per line) and even more quotemarks:
>>
>>print 'x=',x,'self.z=',self.z,...
>>
>>    
>>
>
>ewwww....
>
>  
>
>>But I _hate_ that kind of output for anything longer than
>>two or three lines.  It's nearly impossible to read smoothly
>>something which is say 50 lines of the above with many variables.
>>
>>Cheers, and TIA,
>>
>>f.
>>
>>    
>>
>
>-gus
>
>  
>


-- 
Jeffery Collins (http://www.boulder.net/~jcollins)







More information about the Python-list mailing list