Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:45:58+00:00 2026-06-14T08:45:58+00:00

Context: I’m making a Ren’py game. The value is Character(). Yes, I know this

  • 0

Context: I’m making a Ren’py game. The value is Character(). Yes, I know this is a dumb idea outside of this context.

I need to create a variable from an input string inside of a class that exists outside of the class’ scope:

class Test:
    def __init__(self):
        self.dict = {} # used elsewhere to give the inputs for the function below.

    def create_global_var(self, variable, value):
        # the equivalent of exec("global {0}; {0} = {1}".format(str(variable), str(value)))
        # other functions in the class that require this.

Test().create_global_var("abc", "123") # hence abc = 123

I have tried vars()[], globals()[variable] = value, etc, and they simply do not work (they don’t even define anything) Edit: this was my problem.

I know that the following would work equally as well, but I want the variables in the correct scope:

setattr(self.__class__, variable, value) # d.abc = 123, now. but incorrect scope.

How can I create a variable in the global scope from within a class, using a string as the variable name, without using attributes or exec in python?

And yes, i’ll be sanity checking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:45:59+00:00Added an answer on June 14, 2026 at 8:45 am

    First things first: what we call the "global" scope in Python is actually the "module" scope
    (on the good side, it diminishes the "evils" of using global vars).

    Then, for creating a global var dynamically, although I still can’t see why that would
    be better than using a module-level dictionary, just do:

    globals()[variable] = value
    

    This creates a variable in the current module. If you need to create a module variable on the module from which the method was called, you can peek at the globals dictionary from the caller frame using:

    from inspect import currentframe
    currentframe(1).f_globals[variable] = name
    

    Now, the this seems especially useless since you may create a variable with a dynamic name, but you can’t access it dynamically (unless using the globals dictionary again)

    Even in your test example, you create the "abc" variable passing the method a string, but then you have to access it by using a hardcoded "abc" – the language itself is designed to discourage this (hence the difference to Javascript, where array indexes and object attributes are interchangeable, while in Python you have distinct Mapping objects)

    My suggestion is that you use a module-level explicit dictionary and create all your
    dynamic variables as key/value pairs there:

    names = {}
    class Test(object):
        def __init__(self):
            self.dict = {} # used elsewhere to give the inputs for the function below.
    
        def create_global_var(self, variable, value):
             names[variable] = value
    

    (on a side note, in Python 2 always inherit your classes from "object")

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context: In Lua, it's trivial and very cheap (4kb of memory) to create a
Context I have this piece of Java Code btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent
Context When iterating over a set of Rdata files (each containing a character vector
Context I want to make this call via reflection instanceOfEventPublisher.Publish<T>(T eventInst); When I call
Context: I'm making a simple solar system simulation in c++/OpenGL. Research: I've tried searching
Context I need to build a formset to allow opening hours to be set
Context I packaged Boost C++ libraries into a framework (for iOS) via this script
Context : programming a c/c++ win32-mfc library How to know whether we are in
Context We have this application, using about 60 coding projects. We have several products
Context : In a game, when each level is completed, a Rating is stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.