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
  Song Modifying Program Starting Point BadenJaden 0 89 Jul-30-2019, 11:28 PM
Last Post: BadenJaden
Question Why does modifying a list in a for loop not seem to work? umut3806 2 170 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Global variable does not seem to be global. Columbo 6 229 Jul-15-2019, 11:00 PM
Last Post: Columbo
  passing an argument to avoid a global Skaperen 9 350 Jul-12-2019, 11:07 PM
Last Post: Skaperen
  including the white space parts in str.split() Skaperen 6 281 Jun-20-2019, 06:03 PM
Last Post: Skaperen
  replace white space with a string, is this pythonic? Skaperen 1 157 Jun-18-2019, 11:36 PM
Last Post: metulburr
  How to remove space between strings sunnyarora 2 222 May-03-2019, 11:44 AM
Last Post: perfringo
  strip space from end of a row of text ineuw 4 246 Apr-15-2019, 03:14 AM
Last Post: ineuw
  Passing a local variable outside of a function malonn 8 327 Apr-15-2019, 01:50 AM
Last Post: malonn
  blank space + input() ClassicalSoul 6 384 Mar-06-2019, 09:50 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)