回答編集履歴
2
追記
test
CHANGED
@@ -171,3 +171,15 @@
|
|
171
171
|
139676165093680
|
172
172
|
|
173
173
|
```
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
**追記: **
|
178
|
+
|
179
|
+
よく考えたら (むしろよく考えなくても) idを見る必要は無いですね。
|
180
|
+
|
181
|
+
せっかくspamの値を切り替えているのですから...
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
無意味な実験をこっそり消すのもなんとも虚しいので、残してはおきます。
|
1
追記
test
CHANGED
@@ -43,3 +43,131 @@
|
|
43
43
|
|
44
44
|
|
45
45
|
グローバルな空間にあるspamをいくら書き換えたところで、2番のspam には影響しません。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
実験
|
50
|
+
|
51
|
+
---
|
52
|
+
|
53
|
+
idを見てみるとなかなか面白いですね。
|
54
|
+
|
55
|
+
```Python
|
56
|
+
|
57
|
+
def debug_print(message, obj):
|
58
|
+
|
59
|
+
print(message)
|
60
|
+
|
61
|
+
print(f'\t{obj}\n\t{id(obj)}')
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
def scope_test():
|
66
|
+
|
67
|
+
def do_local():
|
68
|
+
|
69
|
+
spam = "local spam"
|
70
|
+
|
71
|
+
debug_print("In do_local:", spam)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def do_nonlocal():
|
76
|
+
|
77
|
+
nonlocal spam
|
78
|
+
|
79
|
+
spam = "nonlocal spam"
|
80
|
+
|
81
|
+
debug_print("In do_nonlocal:", spam)
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
def do_global():
|
86
|
+
|
87
|
+
global spam
|
88
|
+
|
89
|
+
spam = "global spam"
|
90
|
+
|
91
|
+
debug_print("In do_global:", spam)
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
spam = "test spam"
|
96
|
+
|
97
|
+
debug_print("Before any assignment:", spam)
|
98
|
+
|
99
|
+
do_local()
|
100
|
+
|
101
|
+
debug_print("After local assignment:", spam)
|
102
|
+
|
103
|
+
do_nonlocal()
|
104
|
+
|
105
|
+
debug_print("After nonlocal assignment:", spam)
|
106
|
+
|
107
|
+
do_global()
|
108
|
+
|
109
|
+
debug_print("After global assignment:", spam)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
scope_test()
|
114
|
+
|
115
|
+
debug_print("In global scope:", spam)
|
116
|
+
|
117
|
+
```
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/HMgzVhh6wCVUg2zs)
|
122
|
+
|
123
|
+
```plain
|
124
|
+
|
125
|
+
Before any assignment:
|
126
|
+
|
127
|
+
test spam
|
128
|
+
|
129
|
+
139676165093808
|
130
|
+
|
131
|
+
In do_local:
|
132
|
+
|
133
|
+
local spam
|
134
|
+
|
135
|
+
139676165093296
|
136
|
+
|
137
|
+
After local assignment:
|
138
|
+
|
139
|
+
test spam
|
140
|
+
|
141
|
+
139676165093808
|
142
|
+
|
143
|
+
In do_nonlocal:
|
144
|
+
|
145
|
+
nonlocal spam
|
146
|
+
|
147
|
+
139676165093488
|
148
|
+
|
149
|
+
After nonlocal assignment:
|
150
|
+
|
151
|
+
nonlocal spam
|
152
|
+
|
153
|
+
139676165093488
|
154
|
+
|
155
|
+
In do_global:
|
156
|
+
|
157
|
+
global spam
|
158
|
+
|
159
|
+
139676165093680
|
160
|
+
|
161
|
+
After global assignment:
|
162
|
+
|
163
|
+
nonlocal spam
|
164
|
+
|
165
|
+
139676165093488
|
166
|
+
|
167
|
+
In global scope:
|
168
|
+
|
169
|
+
global spam
|
170
|
+
|
171
|
+
139676165093680
|
172
|
+
|
173
|
+
```
|