Python Forum
namespaces in comprehension in exec()
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
namespaces in comprehension in exec()
#4
(Mar-27-2017, 10:11 AM)Skaperen Wrote: it doesn't have to be locals.  it just has to be somewhere.  if nothing else is added to to the space then there is the option to extract "misspelled" config options.  but the global space gets stuff added so it is to be avoid if that option is to be used.  my pyconf() function was intended to return a dictionary of what the config file set.  so, the issue of how comprehensions are used is going to be used will be the same in a python format config file as in regular code.  no big deal.

Isolation of executed code can be achieved just with globals parameter (globals parameter has nothing common with global scope (globals()) from where you use exec). And with regular import comprehensions would work, problem is with locals parameter enforcing "class definition execution".

>>> s = "boo=1"
>>> config = {}
>>> exec(s, config)
>>> boo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'boo' is not defined
>>> config['boo']
1
Reply


Messages In This Thread
namespaces in comprehension in exec() - by Skaperen - Mar-27-2017, 07:08 AM
RE: namespaces in comprehension in exec() - by zivoni - Mar-27-2017, 12:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Still not completely clear about namespaces... bytecrunch 3 2,009 Oct-07-2022, 05:44 PM
Last Post: bytecrunch
  How to avoid exec(), globals(), locals(), eval() paul18fr 10 5,188 Apr-21-2021, 05:53 PM
Last Post: snippsat
  exec in a function paul18fr 6 3,470 Apr-19-2021, 11:10 AM
Last Post: paul18fr
  exec + subprocess = UnboundLocalError paul18fr 6 3,589 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  Modules and namespaces (again?) ptrivino 1 1,886 Oct-24-2020, 10:37 PM
Last Post: Larz60+
  exec() in class, NameError niski1996 6 4,076 Apr-20-2020, 07:14 PM
Last Post: niski1996
  Is this use of exec pythonic? psolar 1 1,874 Feb-07-2020, 12:23 PM
Last Post: buran
  problem using exec to save local variables dkarl 0 1,830 Dec-01-2019, 08:52 AM
Last Post: dkarl
  common code, def a function vs exec() a string Skaperen 7 3,465 May-27-2019, 10:13 AM
Last Post: heiner55
  Parse XML with Namespaces dwill 7 20,622 Apr-12-2018, 09:22 PM
Last Post: dwill

Forum Jump:

User Panel Messages

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