Storing a big amount of path names

Chris Angelico rosuav at gmail.com
Thu Feb 11 22:49:15 EST 2016


On Fri, Feb 12, 2016 at 2:13 PM, MRAB <python at mrabarnett.plus.com> wrote:
> Apart from all of the other answers that have been given:
>
>>>> p1 = 'foo/bar'
>>>> p2 = 'foo/bar'
>>>> id(p1), id(p2)
> (982008930176, 982008930120)
>>>> d = {}
>>>> id(d.setdefault(p1, p1))
> 982008930176
>>>> id(d.setdefault(p2, p2))
> 982008930176
>
> The dict maps equal strings (dirnames) to the same string, so you won't have
> multiple copies.

Simpler to let the language do that for you:

>>> import sys
>>> p1 = sys.intern('foo/bar')
>>> p2 = sys.intern('foo/bar')
>>> id(p1), id(p2)
(139621017266528, 139621017266528)

ChrisA



More information about the Python-list mailing list