Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Make the Maze Self-Contained

00:00 Making Maze Self-Contained. To make your Maze class more self-contained, you may augment it with .load() and .dump() methods that will delegate processing to their respective counterparts in the serializer module. However, doing so without any modifications would inevitably lead to the dreaded circular dependency, which Python doesn’t allow.

00:22 You can’t have your class depend on functions in another module and the other way round at the same time. There are a few ways to work around this problem, and each has its pros and cons.

00:34 The most elegant solution is to break the two-way dependency in one direction. Since you want to call the serializing code from within your class, removing all Maze class references from the serializer module is the only option.

00:50 Start refactoring the code by deleting the import statement of Maze from the serializer module.

01:04 As soon as you do that, your code editor should flag all unresolved Maze class references in the module. You can see the first one in the dump() function at the top of the file.

01:15 The function currently accepts a maze object as an argument. You can replace this with intermediary data transfer objects, such as Python built-in types or other models.

01:26 Rather than serializing a Maze instance, you’ll now serialize its building blocks, which the Maze class can put back together.

01:39 Notice that you renamed the function to convey its purpose more accurately by emphasizing its new interface. This change must also cascade down to the serialize() function, while compress() and decompress() remain intact.

02:50 With this update, dump_squares() and serialize() have compatible function signatures once more. You’ll modify your deserializing code in a similar way by getting rid of the dependency on Maze from both load() and deserialize().

03:25 The loading function is now named load_squares() to follow a consistent naming convention, and it returns an iterator of squares rather than a complete maze.

03:35 Additionally, you simplified the other function, deserialize(), by turning it into a generator function, which returns a generator of Square instances.

04:20 You’re now free to make edits to the Maze class to reflect the changes you’ve performed in the serializer module, and that’s what you’ll be looking at in the next section of the course.

Become a Member to join the conversation.