On Nov 24, 10:35 am, Licheng Fang <[EMAIL PROTECTED]> wrote: > Thanks. Then, is there a way to make python treat all strings this > way, or any other kind of immutable objects?
The word generally used is 'atom' when referring to strings that are set up such that 'a == b' implies 'a is b'. This is usually an expensive process, since you don't want to do it to strings that are, e.g., read from a file. Yes, it could be done only for string constants, and some languages (starting with LISP) do this, but that isn't what you (or most people) want. Whether you realize it or not, you want control over the process; in your example, you don't want to do it for the lines read from your file, just the trigrams. The example that I gave does exactly that. It adds a fixed amount of storage for each string that you 'intern' (the usual name given to the process of generating such a string. Let's look at my code again: >>> store = {} >>> def atom(str): global store if str not in store: store[str] = str return store[str] Each string passed to 'atom' already exists. We look to see if copy already exists; if so we can discard the latest instance and use that copy henceforth. If a copy does not exist, we save the string inside 'store'. Since the string already exists, we're just increasing its reference count by two (so it won't be reference counted) and increasing the size of 'store' by (an amortized) pair of pointers to that same string. -- http://mail.python.org/mailman/listinfo/python-list