Created
April 8, 2026 18:18
-
-
Save johnwcowan/98f5b500759a72863c1b103db9f6d0ef to your computer and use it in GitHub Desktop.
mysterious a and b behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Contents of a.py: | |
| #!/usr/bin/env python3 | |
| import b | |
| foo = None | |
| def main(): | |
| global foo | |
| foo = 32 | |
| print(f'in a, foo={foo}') | |
| b.b() | |
| if __name__ == "__main__": | |
| main() | |
| Contents of b.py: | |
| import a | |
| def b():\ | |
| print(f'in b, a.foo={a.foo}') | |
| Outputs: | |
| $ python | |
| Python 3.13.5 (main, Jun 25 2025, 18:55:22) [GCC 14.2.0] on linux | |
| Type "help", "copyright", "credits" or "license" for more information. | |
| >>> import a | |
| >>> a.main() | |
| in a, foo=32 | |
| in b, a.foo=32 | |
| >>> quit | |
| $ ./a.py | |
| in a, foo=32 | |
| in b, a.foo=None | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment