Derivative of string as a name of list or dictionary (or class?)
Alex Martelli
aleax at aleax.it
Fri Jul 26 15:28:43 EDT 2002
John Hunter wrote:
...
> of a class, what do you think of the following approach to
> manipulating globals, which I discovered doing a little more reading
> on this issue in the c.l.python archives.
>
> name = 'seq'
> val = ['John', 'Hunter', 'was', 'here']
> globals()[name] = val
> print seq
>
> globals()['print'] = 'Clobber?'
> print seq #no clobber
You couldn't clobber print anyway (it's a keyword) -- the exec
would fail (noisily -- SyntaxError). Here, you're failing
silently instead, in a sense. When you clobber builtins,
they stay clobbered -- e.g., use your first stanza to trample
over name 'globals', and you'll see how well the 2nd one works.
Alex
More information about the Python-list
mailing list