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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:45:29+00:00 2026-06-18T19:45:29+00:00

I have a script as follows from mapper import Mapper class A(object): def foo(self):

  • 0

I have a script as follows

from mapper import Mapper

class A(object):
    def foo(self):
        print "world"

a = A()
a.foo()
Mapper['test']()

with Mapper defined in the file mapper.py:

Mapper = {'test': a.foo}

where I want to define a function call referencing an object not defined in mapper.py, but in the original code. However the code above gives the error

NameError: name 'a' is not defined

which makes kind of sense, as a is not defined in mapper.py itself. However, is it possible to change the code to let the code do the name resolution in the main code itself, or by the use of globals or something?

To solve this problem I could specify the implementation in mapper.py as a text and use eval in the main code, but I would like to avoid the usage of eval.

Additional information:

  • The full definition of the function has to be made in mapper.py
  • It is not known beforehand what the instance a is, or from what clas it is instantiated.
  • 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-18T19:45:30+00:00Added an answer on June 18, 2026 at 7:45 pm

    Barring security holes like eval, it’s not possible to use a name a in mapper.py unless the name is either defined somewhere in mapper.py or imported from another module. There is no way to just let mapper.py automatically and silently access a value a from a different module.

    In addition, if you’re using it just in a dict as in your example, a.foo is going to be evaluated as soon as the dict is created. It’s not going wait until you actually call the function; as soon as it evaluates a.foo to create the dict, it will fail because it doesn’t know what a is.

    You could get around this second problem by wrapping the element in a function (using a lambda for brevity):

    Mapper = {'test': lambda: a.foo}
    

    . . . but this still won’t help unless you can somehow get a to be available inside mapper.py.

    One possibility is to parameterize your Mapper by the “mystery” object and then pass that object in from outside:

    # mapper.py
    Mapper = {'test': lambda a: a.foo}
    
    # other module
    from mapper import Mapper
    Mapper['test'](a)()
    

    Or, similar to what mgilson suggested, you could “register” the object a with Mapper somehow. This lets you pass the object a only once to register it, and then you don’t have to pass it for every call:

    # mapper.py
    Mapper = {'test': lambda a: Mapper['a'].foo}
    
    # other module
    from mapper import Mapper
    Mapper['a'] = a
    Mapper['test']()()
    

    Note the two sets of parentheses at the end there: one set to evaluate the lambda and extract the function you want to call, and the second set to actually call that function. You could do a similar deal by, instead of using Mapper['a'] as the reference, using a module-level variable:

    # mapper.py
    Mapper = {'test': lambda: a.foo}
    
    # other module
    import mapper
    Mapper = mapper.Mapper
    
    mapper.a = a
    Mapper['test']()()
    

    Note that this requires you to do import mapper in order to set the module variable in that other module.

    You could streamline this somewhat by using a custom class for Mapper instead of a regular dict, and having that class do some work in its __getitem__ to look in a “known location” (e.g., read some module variable) to use as a base for evaluating a. That would be a heavier-weight solution though.

    The bottom line is that you simply cannot (again, without the use of eval or other such holes) write code in mapper.py that uses an undefined variable a, and then define a variable a in another module and have mapper.py automatically know about that. There has to be some line of code somewhere that “tells” mapper.py what value of a you want it to use.

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

Sidebar

Related Questions

I have a script that follows that is supposed to collect data from a
I have the php script as follows : $sql = SELECT * FROM tbl_messages
I use Hibernate and mySql. I have defined the db script as follows: CREATE
I have created a 'handler' python script as follows: import SimpleHTTPServer import SocketServer PORT
I have a script that animates an element as follows: var item_height = $('#item').height();
I have a php script to create jpg thumbnail of pdf as follows; <?php
I have 10 variables. q1 through q10 my script is as follows: if (q1
The code that I have is as follows: <!DOCTYPE html> <html> <head> <script src=javascript/jquery.js
I have a script which I need to invoke as root user from capistrano,
I have a vba script which is supposed to copy data from one sheet

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.