<div class="gmail_quote">On 25 September 2012 23:10, Tim Chase <span dir="ltr"><<a href="mailto:python.list@tim.thechases.com" target="_blank">python.list@tim.thechases.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 09/25/12 16:17, Oscar Benjamin wrote:<br>
> I don't know whether it would be better or worse but it might be<br>
> worth seeing what happens if you replace the FileContext objects<br>
> with tuples.<br>
<br>
</div>If tuples provide a savings but you find them opaque, you might also<br>
consider named-tuples for clarity.<br></blockquote><div><br></div><div>Do they have the same memory usage?</div><div><br></div><div>Since tuples don't have a per-instance __dict__, I'd expect them to be a lot lighter. I'm not sure if I'm interpreting the results below properly but they seem to suggest that a namedtuple can have a memory consumption several times larger than an ordinary tuple.</div>
<div><br></div><div>>>> import sys</div><div>>>> import collections</div><div>>>> A = collections.namedtuple('A', ['x', 'y'])</div><div>>>> sys.getsizeof(a)</div>
<div>72</div><div>>>> sys.getsizeof(A(1, 2))</div><div>72</div><div>>>> sys.getsizeof((1, 2))</div><div>72</div><div>>>> sys.getsizeof(A(1, 2).__dict__)</div><div>280</div><div>>>> A(1, 2).__dict__</div>
<div>OrderedDict([('x', 1), ('y', 2)])</div><div>>>> sys.getsizeof((1, 2).__dict__)</div><div>Traceback (most recent call last):</div><div>  File "<stdin>", line 1, in <module></div>
<div>AttributeError: 'tuple' object has no attribute '__dict__'</div><div><div>>>> A(1, 2).__dict__ is A(3, 4).__dict__</div><div>False</div></div><div><br></div><div>Oscar</div></div>