explain this

Steve Holden sholden at holdenweb.com
Tue Jul 3 15:41:10 EDT 2001


"gods1child" <alankarmisra at hotmail.com> wrote in message
news:25b2e0d9.0107031136.5dc0dad2 at posting.google.com...
> In the code below, why can i add elements to locals but not delete them?
>
> >>> def function(argument):
> ... print locals()
> ... modify(locals())
> ... print locals()
> ...
> >>> def modify(locals):
> ... del locals['argument']
> ... locals['new_argument'] = 'new_value'
> ...
> >>> function(10)
> {'argument': 10}
> {'argument': 10, 'new_argument': 'new_value'}

Because locals() isn't really the local namespace, it *represents* the
current local symbol table. From section 2.3 of the Python Library
Reference:

"""locals ()
Return a dictionary representing the current local symbol table. Warning:
The contents of this dictionary should not be modified; changes may not
affect the values of local variables used by the interpreter.
"""

Ignore such warnings at your peril :-)

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list