I'm creating a library in Python 2.7. Since I'm going to be calling this library (and others like it) like this many, many times, I need to make sure I'm not creating a bunch of objects (like gm below) which will end up wasting memory when they are no longer needed.
However, I'm a bit confused about the fact that with my code, the gm object still exists outside of the with:
import mylib
with mylib.GeneralMethods() as gm:
print gm.say_hello()
>>> hello world
print gm # this is no longer needed, but still exists...
>>> <mylib.GeneralMethods object at 0x10f9c5bd0>
Is there anything I can do to further optimize memory management when designing and calling my library?
I don't want to have stray objects eating memory, like gm.
Right now, this is what my library looks like (heavily simplified):
class GeneralMethods(object):
def __init__(self, parent=None):
super(GeneralMethods, self).__init__()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
return isinstance(value, TypeError)
def say_hello(self):
return 'hello world'