How can I create customized classes that have similar properties as 'str'?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Nov 25 05:53:50 EST 2007


On Sun, 25 Nov 2007 10:39:38 +0100, Peter Otten wrote:

> So if you are going to submit Sam's function make sure to bundle it with
> this little demo...

Well Peter, I was going to reply with a comment about not changing the 
problem domain (tuples of ints to trigrams from a text file for natural 
language processing, that is, three character alphanumeric strings), and 
that if you re-did your test with strings (as I did) you would see 
absolutely no difference. What I was going to say was "Tuples aren't 
interned. Short strings that look like identifiers are. Jumping through 
hoops to cache things which are already cached is not productive 
programming."

But then I dug a little deeper, and disassembled the code I was running, 
and discovered that I was being fooled by the Python compiler's constant-
folding, and if I took steps to defeat the optimizer, the effect I was 
seeing disappeared, and I got the same results as you.

Well. So I've learned something new: Python doesn't intern strings in the 
way I thought it did. I don't quite know *how* it decides which strings 
to intern and which ones not to, but at least I've learnt that what I 
thought was true is not true.

So I offer my apology to Samwyse, the caching code isn't as redundant and 
silly as it appears, and humbly tuck into this nice steaming plate of 
crow.

Somebody pass the salt please.



-- 
Steven.



More information about the Python-list mailing list