Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

10
  • 1
    -1: saying it twice doesn't make it a good idea. Modifying globals() is bad, and should only be done in very specialized circumstances, usually involving coding tools. If you need a dynamic set of named values, use a dictionary, it's what they're there for. Commented Feb 18, 2011 at 12:34
  • 1
    Why add to globals() just to have to turn around and keep a separate list of all the names you dumped in there? A dictionary will handle all of that for you, in a cleaner, simpler, more understandable, more flexible, and safer way. Just because you can do something doesn't mean you should do something. Commented Feb 18, 2011 at 12:35
  • 1
    @Ned Batchelder Downvoting twice doesn't make me undertsand more. I don't pretend that these two codes are perfect arguments: they are only answers to criticism made by free-dom on 2 points. They don't want to prove that it is good to use globals()['X'] to create a new object with name X that will be used as following: li.append(X) etc etc; Commented Feb 18, 2011 at 16:33
  • 2
    @Ned Batchelder Now, thanks to the answer and the CODE of John Machin, I know another manner: creating a new attribute in an instance with the name X, using setattr() : that's fine. Or I understand nothing. Until now, I wrote several codes, and John Machin also, and Duncan gave me precise explanations, while you only merely contented to downvote twice and to emit dogmatic sentence that Modifying globals() is bad Your downvotes begin to bore me and they don't make me understand Commented Feb 18, 2011 at 16:44
  • 3
    @Ned Batchelder Moreover, modifying globals() can't be bad in the absolute, since it IS modified when we just wrote v = 48 or for i in xrange(20) . I think that it's for a great part because writing globals()["v"] = 48 looks long and weird that it is badly considered. But where is the real weirdness in that ? I don'y understand. Commented Feb 18, 2011 at 16:53