472,989 Members | 3,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

YA string interpolation and printing idea

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])
Jul 18 '05 #1
4 1400
Paul Rubin wrote:
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'.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ It comes from inside, and that's what I consider to be soul music.
-- Sade Adu
Jul 18 '05 #2
Erik Max Francis <ma*@alcyone.com> writes:
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.
Jul 18 '05 #3
On 16 Jan 2004 20:52:08 -0800, rumours say that Paul Rubin
<http://ph****@NOSPAM.invalid> might have written:
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.
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix
Jul 18 '05 #4
Christos "TZOTZIOY" Georgiou <tz**@sil-tec.gr> wrote in message news:<gk********************************@4ax.com>. ..
On 16 Jan 2004 20:52:08 -0800, rumours say that Paul Rubin
<http://ph****@NOSPAM.invalid> might have written:
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.


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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Eli Daniel | last post by:
Hi, I'm new to Python. Can you please tell me if the following is possible. My users are using other scripting launguage to write scripts. They are used to write somthing like (keeping it...
2
by: Michele Simionato | last post by:
This is somewhat in between a bug report and a feature request. I was using the new string.Template class and I have run into a few issues that I traced back to the usage of the idiom '%s' % val...
14
by: Charles Banas | last post by:
I'm not sure if this is the right place to ask about this, but I've seen several posts in the past regarding Akima's Bivariate Interpolations routines, and i'm wondering if someone can give me some...
3
by: Jonas Ernst | last post by:
Hi, Can somebody give me some hints how to do a line interpolation without using floating point arithemtics? The function shall do a linear interpolation between 2 points (line interp?) and...
2
by: Kun | last post by:
I have an html form that takes dates and inserts them into a mysql file. Currently, users have to type in dates in the yyyy-mm-dd format. As of now, this process works with the sql. However, I...
5
by: xandra | last post by:
i understood the concept of interpolation search. but i couldn't understand what would be the steps for that search. for example, if i'm searching for J in this file A A B E F H J M N N N N O P P...
0
by: MonkeeSage | last post by:
There are several string interpolation functions, as well as string.Template. But here's yet another. This one emulates ruby's inline interpolation syntax (using #{}), which interpolates strings as...
3
by: Sean Farrow | last post by:
Hi: I have the folling code: def parseTime(self, time): minutes =int(float(time)/60) seconds =int(float(time)-minutes*60) minutes =str(minutes) seconds =str(minutes) the statements that...
6
by: Patient Guy | last post by:
I am a newcomer to using PHP but not to programming (C, C++, Javascript). I am playing around with classes and wanted to make a function that has a method simply for producing either plain text...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.