[英]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. lookup
功能的目的是在字典中查找x
并返回值。 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.
我想知道是否在类中声明dict,还是将dict声明为全局变量。
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. 我担心内存效率:dict将保持不变,我希望它不占用比所需更多的内存。 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
? 如果我声明了50,000个
Foo
实例,并且在Foo
声明了该命令,那是否意味着我将产生50,000个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. 每个对象本质上都有一个dict,如果您希望提高内存效率,则可以使用一个对象存储所有50k(而不是50k)对象。
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. 如果您有一个数据存储对象,该对象使用名称或可能的索引作为
get
的参数以及set
的值,则只有一个字典可以存储所有值。 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. 当然,如果在set方法中使用
setattr(self, name, value)
,它将允许您以obj.name
访问值-唯一的事情是python已经为您完成了此操作。
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. 在这种情况下,您应该将
dict
作为全局变量。 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. 这就是每个
Foo
实例都有其自己的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. (实际上命名为
self.dict
就像您应该在使用对象的内部变量的lookup
函数中将self.x
作为自变量。)使用全局变量既可以节省内存,又可以防止程序中使用不同版本的dict的可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.