$ python ./main.py
PRE-CHANGE
First (direct): I am first
First (attribute): I am first
Second (direct): I am second
Second (attribute): I am second
CHANGE 1
First (direct): I am first
First (attribute): I am first
Second (direct): I am second
Second (attribute): I am second
CHANGE 2
First (direct): I am first
First (attribute): I am first
Second (direct): I am second
Second (attribute): changed
FOLLOW-UP HYPOTHESIS
First (direct): I am first
First (attribute): changed
Second (direct): I am second
Second (attribute): changed
Last active
June 12, 2019 00:17
-
-
Save TheLonelyGhost/9dbe810c42d8f2edcf3388a8b19519e1 to your computer and use it in GitHub Desktop.
Global state versus variable shadowing (python)
This file contains 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
first = 'I am first' | |
second = 'I am second' |
This file contains 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
from __future__ import print_function | |
import global_state | |
from global_state import first, second | |
from mutator import change_first, change_second, change_first_alt | |
print('PRE-CHANGE') | |
print('First (direct): {}'.format(first)) | |
print('First (attribute): {}'.format(global_state.first)) | |
print('Second (direct): {}'.format(second)) | |
print('Second (attribute): {}'.format(global_state.second)) | |
change_first() | |
print('CHANGE 1') | |
print('First (direct): {}'.format(first)) | |
print('First (attribute): {}'.format(global_state.first)) | |
print('Second (direct): {}'.format(second)) | |
print('Second (attribute): {}'.format(global_state.second)) | |
change_second() | |
print('CHANGE 2') | |
print('First (direct): {}'.format(first)) | |
print('First (attribute): {}'.format(global_state.first)) | |
print('Second (direct): {}'.format(second)) | |
print('Second (attribute): {}'.format(global_state.second)) | |
change_first_alt() | |
print('FOLLOW-UP HYPOTHESIS') | |
print('First (direct): {}'.format(first)) | |
print('First (attribute): {}'.format(global_state.first)) | |
print('Second (direct): {}'.format(second)) | |
print('Second (attribute): {}'.format(global_state.second)) |
This file contains 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
import global_state | |
from global_state import first | |
def change_first(): | |
first = 'changed' | |
def change_second(): | |
global_state.second = 'changed' | |
def change_first_alt(): | |
global_state.first = 'changed' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment