简体   繁体   中英

How to be memory-efficient with variables in classes?

Suppose, for the sake of example, that I have a class like this:

def class Foo(object):
    def __init__(self,x):
        self.x = x
    def lookup(self):
        return dict[x]

The purpose of the lookup function is to look up x in a dict and return the value. Suppose that this dict is very large. I am wondering whether to declare the dict in the class, or whether to declare the dict as a global variable.

I am worried about memory-efficiency: the dict is going to be constant, and I want it not to take more memory than it would need. Thus, I am asking about how classes use memory. If I declare eg 50,000 instances of Foo , and I declare the dict within Foo , does that mean I am spawning 50,000 copies of Foo ? Whereas if I refer to the dict as a global variable, I will not spawn any additional copies?

How do I make as few copies of the dict as possible? Ideally, this program would have just one.

Every object has a dict inherently, if you wish to be memory efficient you have a single object that stores all 50k rather than 50k objects.

If you have a data store object which takes a name, or possibly and index, as a parameter for for get as well as a value for set you will only have one dictionary which will store all your values. Of course if you use setattr(self, name, value) in your set method it will allow you to access the values as obj.name - the only thing is that python already does that for you.

In [4]: class DS(object):
   ...:     def __init__(self):
   ...:         pass
   ...:

In [5]: ds = DS()

In [6]: ds.a = 'A'

In [7]: ds.b = 2

In [8]: ds
Out[8]: <__main__.DS at 0x3c82db0>

In [9]: ds.a
Out[9]: 'A'

In [10]: ds.b
Out[10]: 2

In [11]: dir(ds)
Out[11]:
['__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'a',
 'b']

In this case you should have dict as global variable. The idea of inner variables is that you can change and use them independently without clashes. That's why every instance of Foo would have it's own copy of dict. (Actually named self.dict - like you should have self.x as an argument in your lookup function which uses object's inner variable.) Using global variable both saves memory and prevents the possibility that there might be different versions of dict in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM