1

Say I want to iterate over a list and create a new variable at each iteration. Creating a new list and using append() is not always the most convenient output, as it might be easier to directly access each new variable. A possible solution might be using globals(). For example:

for i,n in enumerate(["Amy", "Luis", "Jerry"]):
    globals()["person_" + str(i)] = "my name is " + n

The output:

person_0
'my name is Amy'

person_1
'my name is Luis'

person_2
'my name is Jerry'

This seems like a very simple solution. However, I have seen advices against using globals(), but I couldn't find a proper explanation. What is the reason for this? What might be a different solution which doesn't involve using list append?

quamrana
  • 37,849
  • 12
  • 53
  • 71
Luis_12345
  • 55
  • 6
  • 3
    Seems that you could use just a simple dictionary for storing your data. – Niko Föhr Jul 09 '23 at 15:37
  • 3
    Why not use a dictionary for data entries with variable names? – Mathias R. Jessen Jul 09 '23 at 15:38
  • 1
    ... and if you want a collection of values identified by sequential integer indexes, then why *not* use a list? I suspect you haven't really thought it through when you suppose that what you present is in any way easier than that. – John Bollinger Jul 09 '23 at 15:41
  • 1
    Why do you _need_ actual separate variables? Why isn't a simple list of names good enough? – John Gordon Jul 09 '23 at 15:45
  • I think @JohnGordon just asked the correct question. Usually the name of the variable does not matter. The data the variable holds matters. Typically you would like to create some "Person" objects and put them to a list. – Niko Föhr Jul 09 '23 at 15:48

1 Answers1

-1

A lot depends on the wider context.

If this is a small script (let's say up to 200 lines) that you'll run once or twice, feel free to do whatever you want.

If this is a project that will grow over time then it'll get very hard managing global mutable state. Imagine you have 5 places where you're changing person_0. What happens if one of the 5 places has a bug? You'll have a hard time figuring out where's the issue.
-- to clarify, projects can have tens of thousands of lines and anything anywhere is a suspect :)

Having lots of global state is also not good because you might allocate memory that can't be freed. One big thing about functions is that variables within their scope usually get deleted once the function ends.

So in the end, it really depends on the context.

Disclaimer: Be advised that, if you're a beginner, you might want to apply the best practices in order to learn.

Spidey
  • 894
  • 1
  • 13
  • 29
  • OP is clearly a novice. Even with small scripts (used for learning the language) it's far better to use "best practice" techniques. – DarkKnight Jul 09 '23 at 16:00
  • 1
    OP asked for an explanation. Just telling him/her "this is the way" is teaching him/her absolutely nothing. I've seen developers with experience wasting hours/days writing 1-time scripts. That said, I'll add a disclaimer that a novice might want to apply best practices to learn. – Spidey Jul 09 '23 at 16:22