YA string interpolation and printing idea

P

Paul Rubin

How about adding a string interpolation method and a some print
methods to strings.

'hello world'.print([fd]) => same as "print 'hello world'". With optional
fd arg, print to file object
'hello world'.write([fd]) => same as "sys.stdout.write('hello world')"
i.e. print with no trailing newline
'hello $name'.interp() => like "'hello %s'%name"
'hello $name'.fprint([fd]) => like ('hello %s'%name).print([fd])
'hello $name'.fwrite([fd]) => like ('hello %s'%name).write([fd])
 
E

Erik Max Francis

Paul said:
How about adding a string interpolation method ...

You already have one with the % operator:

"hello %(name)s" % locals()
... and a some print
methods to strings.

Why in the world would you want that? Printing methods go on the things
that do work of printing, which are file-like objects, not strings.
And, on file-like objects, that method is called `write'.
 
P

Paul Rubin

Erik Max Francis said:
Why in the world would you want that? Printing methods go on the things
that do work of printing, which are file-like objects, not strings.

To give a convenient way to do interpolated printing. Practicality
beats purity. Another approach I like is to define a unary minus
operation on strings, which does interpolation. But that's gone
nowhere when I've proposed it in the past. Maybe if type/object
unification proceeds far enough, we'll someday be able to define our
own operations on builtin objects like strings.
And, on file-like objects, that method is called `write'.

'write' does what it should, which is writes out exactly the
characters you give it. Python's print statement does the same plus
adds a newline, and people have gotten used to that misbehavior
(explicit is better than implicit), so I made an accomodation for that.
 
C

Christos TZOTZIOY Georgiou

Maybe if type/object
unification proceeds far enough, we'll someday be able to define our
own operations on builtin objects like strings.

I'd like that too, and I believe Ruby does it; however, ISTR that Guido
has specifically said that this won't be allowed, since you can always
subclass the base types. He's the boss :)

I've also seen a hackish patch (by MWH? can't be sure) that allowed one
to substitute their own classes for the base types (so that integer or
string constants would be instances of the derived subclasses). Don't
know if that would help you in your code.
 
M

Michele Simionato

Christos "TZOTZIOY" Georgiou said:
I'd like that too, and I believe Ruby does it; however, ISTR that Guido
has specifically said that this won't be allowed, since you can always
subclass the base types. He's the boss :)

I've also seen a hackish patch (by MWH? can't be sure) that allowed one
to substitute their own classes for the base types (so that integer or
string constants would be instances of the derived subclasses). Don't
know if that would help you in your code.

In Python, you cannot change built-in types, but you can always derive
a new type with additional methods and give to the new type the name
of a built-in type. So, we don't miss much of Ruby functionality, we just
miss is a bit of sugar. Also, knowing that the builtins types are fixed,
gives me some sense of safety ...

Micjele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top