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

samwyse samwyse at gmail.com
Sat Nov 24 07:54:43 EST 2007


On Nov 24, 5:44 am, Licheng Fang <fanglich... at gmail.com> wrote:
> Yes, millions. In my natural language processing tasks, I almost
> always need to define patterns, identify their occurrences in a huge
> data, and count them. [...] So I end up with unnecessary
> duplicates of keys. And this can be a great waste of memory with huge
> input data.

create a hash that maps your keys to themselves, then use the values
of that hash as your keys.

>>> store = {}
>>> def atom(str):
	global store
	if str not in store:
		store[str] = str
	return store[str]

>>> a='this is confusing'
>>> b='this is confusing'
>>> a == b
True
>>> a is b
False
>>> atom(a) is atom(b)
True



More information about the Python-list mailing list