As it turns out, the issue is a bug with the site pythonsandbox.com that I tried to use for verifying my implementation idea. The problem doesn't happen in the Python code itself, but rather in the site's own structure.