accessing local and global dictionaries

Harri Pasanen (pa@tekla.fi)
01 Mar 1995 13:01:50 GMT

Hello,

How do I access the local and global dictionaries for some block in
a program?

Consider the following function I made,

import string

# Simulate enum, as found in C
def enum(varstr, start):
l = varstr
n = 1
i = string.find(l, ',')
while i >= 0:
n = n + 1
l = l[i+1:]
i = string.find(l, ',')
exec('global '+varstr+'\n['+varstr+']=range(start, start+n)')

# Example usage: define globals a,b and c, starting from 2.
enum('a,b,c', 2)

How could I make this work for local scopes? I know exec can take the
global and local dicts to work with, but how are they called?

Ideally, I'd like the exec above take the local dict of the block
calling enum(), so then I would not have to pass it as an enum()
parameter.

Thanks,

Harri

--
==========================================================================
Harri Pasanen k. 802 5350,   t. 8879 436