473,763 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.wri te('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 1452
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.co m> 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.i nvalid> 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.i nvalid> 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
1632
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 simple) T = 100 do_action ('It cost $T dollars')
2
1442
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 in the 'convert' helper function in Template.substitute. I do not understand why '%s' % val was used instead of just str(val).
14
5201
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 ideas or PD code I can put to use right away. At the moment, I'm maintaining a contour calculation and plotting program for radio wave propagation studies. This program uses a number of routines written by and for the FCC in Fortran in 1976...
3
3907
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 it shall return the y value to any x value given. e.g. p1: (0,0) p2 (1000,50) y value to calculate at x=600
2
2953
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 would like to make this process easier by: 1) providing drop down menus for year, month, and date respectively. in a failed attempt, i tried made 3 drop down lists (dateyear, datemonth, dateday) respectively and then used string interpolation...
5
3812
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 P P P R R R R T T T Z Z Z Z Z Z Z i know that i have to look in the begining of this file. but what is the steps to find j/ thank you for your help
0
1288
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 well as expressions. NB. It uses eval(), so only use it in trusted contexts! import sys, re def interp(string): locals = sys._getframe(1).f_locals globals = sys._getframe(1).f_globals
3
1667
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 convert the minutes and seconds variables str(minutes) and str(seconds) don't seem to be working. Any idea why?
6
3159
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 or HTML output in a tabular way a listing of the global variables in the = keyValue two-column format. That's what the method listGlobals does. I am stumped by the following error message: PHP Catchable fatal error: Object of class phpEnv...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.