Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 modifying variables in local or global space
#1
you can't modify variables in local space using they dictionary you get from locals() or the reference you can track down from inspect.currentframe(). is there any way to do this, at all? can globals be safely modified since all the documents that touch on this topic seem to focus only on locals?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
(Aug-14-2019, 03:55 AM)Skaperen Wrote: you can't modify variables in local space using they dictionary you get from locals()

but.... I can

>>> l=locals()
>>> l["foo"] = "bar"
>>> print(locals())
{'__spec__': None, '__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, 'foo': 'bar', '__package__': None, '__doc__': None, 'l': {...}}
Quote
#3
by "can't" i meant that you are not supposed to according to Python documentation. it is CPython specific according to one source. another source said it is something about how compiled code accesses the namespace at runtime. yes, if you try, it looks like it works. it works most of the time. it sometimes fails.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  local / global lists RedWuff 1 250 May-26-2020, 03:11 AM
Last Post: deanhystad
  Question regarding local and global variables donmerch 12 534 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 352 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  Creating local variables from a string peckjonk 2 298 Feb-15-2020, 06:07 PM
Last Post: ndc85430
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 481 Feb-09-2020, 02:05 PM
Last Post: new_to_python
  Please help me understand how to use global variables joew 6 559 Jan-05-2020, 06:03 PM
Last Post: joew
  problem using exec to save local variables dkarl 0 281 Dec-01-2019, 08:52 AM
Last Post: dkarl
  Global Variables - Some Points Needing Clarification. adt 4 538 Nov-30-2019, 01:23 PM
Last Post: adt
  visibility/space of variables. Pls help. gtlhbkkj 1 244 Oct-12-2019, 05:51 PM
Last Post: buran
  Global variable does not seem to be global. Columbo 6 753 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:


Users browsing this thread: 1 Guest(s)