python - How to be memory-efficient with variables in classes? -
suppose, sake of example, have class this:
def class foo(object): def __init__(self,x): self.x = x def lookup(self): return dict[x]
the purpose of lookup
function x
in dict , return value. suppose dict large. wondering whether declare dict in class, or whether declare dict global variable.
i worried memory-efficiency: dict going constant, , want not take more memory need. thus, asking how classes use memory. if declare e.g. 50,000 instances of foo
, , declare dict within foo
, mean spawning 50,000 copies of foo
? whereas if refer dict global variable, not spawn additional copies?
how make few copies of dict possible? ideally, program have one.
in case should have dict
global variable. idea of inner variables can change , use them independently without clashes. that's why every instance of foo
have it's own copy of dict. (actually named self.dict
- should have self.x
argument in lookup
function uses object's inner variable.) using global variable both saves memory , prevents possibility there might different versions of dict in program.
Comments
Post a Comment