[Tutor] how to make an lexical scope block?
Xiaosong Chen
xiaosong0911 at gmail.com
Sat Aug 5 03:23:57 EDT 2017
In C, it's a common pattern to use temporary variables in an lexical
scope to prevent the global scope from getting dirty.
For example,
```C
int a[N];
for (int i = 0; i < N; i++) {
int temp = ...
a[i] = ... // something got from temp
}
// temp do not exists here
```
But in python, such a pattern seems impossible. An straightforward
translation should be like this:
```python
a = []
for i in range(N):
temp = ...
a.append(...)# something got from temp
# temp DO EXISTS here, will be the value of the last iteration
```
As in the comment, the temporary variable remains existing after the
block. How do you usually deal with this?
More information about the Tutor
mailing list