Micro-PEP: str.translate(None) to mean identity translation

B

Bengt Richter

Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting characters,
e.g.,

s = s.translate(None, 'badcharshere')

Regards,
Bengt Richter
 
M

Michael Spencer

Bengt said:
Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting characters,
e.g.,

s = s.translate(None, 'badcharshere')

Regards,
Bengt Richter

+1

Michael
 
M

M.E.Farmer

Bengt said:
Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting characters,
e.g.,

s = s.translate(None, 'badcharshere')

Regards,
Bengt Richter

What's wrong with :

s = s.replace('badchars', "")

It seems obvious to replace a char ( to me anyway ) with a blank
string, rather than to 'translate' it to None.
I am sure you have a reason what am I missing ?
M.E.Farmer
 
P

Peter Otten

M.E.Farmer said:
What's wrong with :

s = s.replace('badchars', "")

It seems obvious to replace a char ( to me anyway ) with a blank
string, rather than to 'translate' it to None.
I am sure you have a reason what am I missing ?
M.E.Farmer
"NHoBwA RyAoSuB AsAeHeH AiBtH,A CnRoAwD HyBoAuC HdRoCnH'AtB"

i. e. you have to replace() for every char in "BADCHARS":
"Now you see it, now you don't"


+1, btw.

Peter
 
B

Bengt Richter

What's wrong with :

s = s.replace('badchars', "")
That's not what translate does with the badchars, which is a set of
characters, each and all of whose instances in the source string
will be deleted from the source string. Something like
for c in badchars:
s = s.replace(c,'')
It seems obvious to replace a char ( to me anyway ) with a blank
string, rather than to 'translate' it to None.
I am sure you have a reason what am I missing ?
M.E.Farmer
The first argument is normally a 256-character string that serves as a table
for converting source characters to destination, so s.translate(table, bad)
does something like
s = ''.join([table[ord(c)] for c in s if c not in bad]
Help on method_descriptor:

translate(...)
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.

So just to delete, you wind up constructinf a table argument that is just 1:1 as in
>>> 'abcde'.translate(''.join([chr(i) for i in xrange(256)]), 'tbd')
'ace'

and the answer to the question, "What translation does such a table do?" is "None" ;-)

Regards,
Bengt Richter
 
R

Raymond Hettinger

[Bengt Richter]
Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting characters,
e.g.,

s = s.translate(None, 'badcharshere')

Log a feature request on SF and assignment to me.
I'll put this in for you.


Raymond Hettinger
 
M

M.E.Farmer

After I re-read your post in daylight and read your followup the "Aha!"
hit me .I am a little slow at times. I have always just iterated thru
the badchars and replaced with "" . What you suggest would be very
nice indeed!
Thanks for the time and explanation, and you too Peter !

For what it is worth +1 ;)

On another note why is maketrans in the string module....
I was a bit lost finding it the first time should it be builtin?
transtable = "abcdefg".maketrans("qwertyu")
Probably missing something.
M.E.Farmer
 
B

Bengt Richter

[Bengt Richter]
Just thought None as the first argument would be both handy and mnemonic,
signifying no translation, but allowing easy expression of deleting characters,
e.g.,

s = s.translate(None, 'badcharshere')

Log a feature request on SF and assignment to me.
I'll put this in for you.
Thanks. It has request ID # 1193128

Regards,
Bengt Richter
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top