python: class vs tuple huge memory overhead (?)

后端 未结 4 925
萌比男神i
萌比男神i 2021-02-20 12:16

I\'m storing a lot of complex data in tuples/lists, but would prefer to use small wrapper classes to make the data structures easier to understand, e.g.

class Pe         


        
4条回答
  •  眼角桃花
    2021-02-20 12:53

    In your second example, you only create one object, because tuples are constants.

    >>> l = [('foo', 'bar') for i in range(10000000)]
    >>> id(l[0])
    4330463176
    >>> id(l[1])
    4330463176
    

    Classes have the overhead, that the attributes are saved in a dictionary. Therefore namedtuples needs only half the memory.

提交回复
热议问题