Python Forum
modifying variables in local or global space
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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': {...}}
Reply
#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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,166 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Trying to understand global variables 357mag 5 1,122 May-12-2023, 04:16 PM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,789 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,029 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,150 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  How to use global value or local value sabuzaki 4 1,155 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  Clarity on global variables JonWayn 2 947 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Modifying code cheburashka 1 1,296 Dec-13-2021, 01:01 PM
Last Post: Kebap
  Global variables not working hobbyist 9 4,734 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,655 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020