diff --git a/examples/objects/src/main.py b/examples/objects/src/main.py index 9f9e4cb..70bc14d 100644 --- a/examples/objects/src/main.py +++ b/examples/objects/src/main.py @@ -23,10 +23,7 @@ class ZipCodeDatabase: def __init__(self): self.obj = lib.zip_code_database_new() - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_value, traceback): + def __del__(self): lib.zip_code_database_free(self.obj) def populate(self): @@ -35,8 +32,8 @@ def populate(self): def population_of(self, zip): return lib.zip_code_database_population_of(self.obj, zip.encode('utf-8')) -with ZipCodeDatabase() as database: - database.populate() - pop1 = database.population_of("90210") - pop2 = database.population_of("20500") - print(pop1 - pop2) +database = ZipCodeDatabase() +database.populate() +pop1 = database.population_of("90210") +pop2 = database.population_of("20500") +print(pop1 - pop2) diff --git a/site/objects/index.md b/site/objects/index.md index caa8ded..bc63758 100644 --- a/site/objects/index.md +++ b/site/objects/index.md @@ -83,11 +83,11 @@ We create an empty structure to represent our type. This will only be used in conjunction with the `POINTER` method, which creates a new type as a pointer to an existing one. -To ensure that memory is properly cleaned up, we use a *context -manager*. This is tied to our class through the `__enter__` and -`__exit__` methods. We use the `with` statement to start a new -context. When the context is over, the `__exit__` method will be -automatically called, preventing the memory leak. +To ensure that memory is properly cleaned up, we define a `__del__` +method on our class for the Python garbage collector to call when it +detects that there are no longer any references to our instance of the +`ZipCodeDatabase` class, which prevents a memory leak. + ## Haskell