Python Forum
How to avoid exec(), globals(), locals(), eval()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to avoid exec(), globals(), locals(), eval()
#8
Are you passing a list of dictionary names because you think this has less overhead than passing a list of dictionaries? That is incorrect. A python dictionary variable is going to be 4 or 8 bytes. A python string variable is going to be 4 or 8 bytes. A list of dictionary names will be the same size as a list of dictionaries.

There are several posts in this forum asking how you can programmatically create new variable names. The poster assumes that since the program manipulates multiple variables that each of the variables must have a name. They are nearly always incorrect. Variable names are mnemonics for writing and reading code. Python does not care what your variables are named. "spam", "eggs", dictlist[0] and dictionaries["eggs"] can all reference a dictionary.

If you know there will only be a "spam" dictionary then it is good to use a variable named "spam". If you have two dictionaries it may make sense to think of using a collection. If you have many dictionaries and the number is variable, a collection is definitely the way to go.
Reply


Messages In This Thread
RE: How to avoid exec(), globals(), locals(), eval() - by deanhystad - Apr-21-2021, 12:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  why globals() exits python when quit() is aliased as q abc12346 4 833 May-23-2023, 08:56 AM
Last Post: Gribouillis
  exec in a function paul18fr 6 3,650 Apr-19-2021, 11:10 AM
Last Post: paul18fr
  exec + subprocess = UnboundLocalError paul18fr 6 3,739 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  int, floats, eval menator01 2 2,551 Jun-26-2020, 09:03 PM
Last Post: menator01
  exec() in class, NameError niski1996 6 4,226 Apr-20-2020, 07:14 PM
Last Post: niski1996
  Is this use of exec pythonic? psolar 1 1,921 Feb-07-2020, 12:23 PM
Last Post: buran
  problem using exec to save local variables dkarl 0 1,873 Dec-01-2019, 08:52 AM
Last Post: dkarl
  common code, def a function vs exec() a string Skaperen 7 3,639 May-27-2019, 10:13 AM
Last Post: heiner55
  eval lambda function with restricted context Olivier 7 5,390 Mar-04-2019, 10:45 PM
Last Post: Olivier
  eval not working in code jamminjamie 0 2,285 Dec-27-2018, 05:24 PM
Last Post: jamminjamie

Forum Jump:

User Panel Messages

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