Yet another string formatting proposal (Was: python-dev Summary for 2002-11-16 through 2002-11-30)

Bengt Richter bokr at oz.net
Tue Dec 3 23:39:21 EST 2002


On Wed, 04 Dec 2002 15:35:11 +1300, Greg Ewing <see_reply_address at something.invalid> wrote:

>Mike Rovner wrote:
>
>> We already have print notion for that purpose. Instead of introducing
>> another one, IMHO better extend print to output to a string.
>> Syntax may be like
>>     print >>var, <expr-list>
>> or we can do better.
>
>
>Actually, that's already taken -- it means to
>print to a file.
Or something that has a write method. You knew that ;-)

>
>Possibly it could be
>
>   print >>$var, ...
>
No need:

 >>> class Var:
 ...     def __init__(self): self.sl = []
 ...     def write(self, s): self.sl.append(s)
 ...     def __repr__(self): return ''.join(self.sl)
 ...
 >>> var = Var()
 >>> print >> var, 'Hi','ho',1,2,3,'-- no prob.'
 >>> var
 Hi ho 1 2 3 -- no prob.

 >>> print >> var, 'Some more'
 >>> var
 Hi ho 1 2 3 -- no prob.
 Some more

 >>> var = Var()
 >>> print >> var, 'Brand new'
 >>> var
 Brand new


>But I doubt that would satisfy those who advocate
>a string-interpolation mechanism, because they're
>after something that's more readable than what
>print gives you.
>
Yup.

Regards,
Bengt Richter



More information about the Python-list mailing list