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 8763645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:49:37+00:00 2026-06-13T15:49:37+00:00

I am trying to enable the user of my drawing program (written in python

  • 0

I am trying to enable the user of my drawing program (written in python with pygame) to rename selected objects, then be able to access them through the live interpreter by their names. The following is a method of the base class of all drawable objects. When this code is run, I get no errors, but when I try to access the object through its new name, I am told that the variable with that name is not defined.
Any ideas why this might be?

def rename(self,newName):

    """
    Gives this drawable a new name by which the user my reference it in
    code
    """

    #Create a new variable in the global scope
    command =  'global ' + newName + '\n'

    #Tie it to me
    command += newName + ' = self' + '\n'

    #If I already had a name, I'll remove this reference
    if self.name != None:
        command += 'del ' + self.name

    #Execute the command
    exec(command)

    #Make this adjustment internally
    self.name = newName
  • 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-13T15:49:38+00:00Added an answer on June 13, 2026 at 3:49 pm

    I don’t think that will work, because the exec function has its own conecpt of global variables, independent of the one your function sees.

    Generally speaking it is easier to manipulate the dictionary of the module. Note that the global scope is actually the member names of the current module.

    For example, if you are using the __main__ module, to add a variable:

    sys.modules['__main__'].__setattr__('xxx', 42)
    

    And to delete it:

    sys.modules['__main__'].__delattr__('xxx')
    

    UPDATE: On second thought, if you don’t care about the module, it is better if you use the globals() dictionary. To add a variable:

    globals()['xxx'] = 42
    

    And to delete it:

    del globals()['xxx']
    

    Naturally, that is equivalent to the former, because globals() returns something like (sys.modules[__name__].__dict__).

    Nifty. The conclusion is: if you use eval to do reflection, you are doing it wrong.

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

Sidebar

Related Questions

In my program (jldap-based) I trying to enable user in AD by setting userAccountControl
I'm trying to enable a user in the Active Directory with LDAP and Java
I'm trying to enable screenshots of the page a logged in user is currently
I'm trying to disable/enable controls based on user permission using a custom security framework
I'm trying to write an IF ELSE statement to enable shipping, If user doesn't
I am trying to enable link time optimization in g++. My program compiles fine
I'm desperately trying to enable the multiple-property to users of my webside. User should
I'm trying to enable a user to pan up/down and left/right an object in
I am trying to enable SSL on tomcat and user the following command to
I am trying to enable mod_write's rewrite URL and am not able to get

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.