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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:07:45+00:00 2026-06-06T17:07:45+00:00

A bit of a complex problem to describe here: I would like to have

  • 0

A bit of a complex problem to describe here:

I would like to have a function AB(), which maps one input to another, through an intermediate value. More specifically, the function is made up of two function, let’s call A() and B(), which both return dictionaries (ie. A[B[input]] –> result)

This is an example of my current file directory:

packageX/

    __init__.py
    get_part_of_B_expensively.py
    something_that_will_use_convert.py
    convert.py
    packageA/

        __init__.py
        get_the_entirety_of_A_expensively.py

In my convert.py I have the following:

import get_part_of_B_expensively
from packageA import get_the_entirety_of_A_expensively
dict_A = packageA.get_the_entirety_of_A_expensively()

def get_B():

    result = {}
    result = get_part_of_B_expensively() # this is a very expensive operation
    result += get_rest_of_B_inexpensively() # shorthand for adding two dictionaries
    return result # returns a dictionary

dict_B = get_B() # hence why i call it once outside, and not inside AB()

def AB(input):
    return dictA[dictB[input]]

Don’t think this is proper since I can’t initialize dict_B before defining get_B(), and I want AB()’s only parameter to be input, since I want to abstract its implementation. However, I introduce a few variables globally, mainly dict_B and dictA, and I’m not sure if that’s the best way to do it. I can separate get_B() into its own package, but I would still be left calling them somewhere in the global frame. Not sure what the approach to this should be, I want to call A() and B(), which calls a file in the same package) as few times as possible but abstract the function AB().

  • 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-06T17:07:47+00:00Added an answer on June 6, 2026 at 5:07 pm

    If this is all your module is doing, I wouldn’t say it’s that bad to keep it that way. For small scripts that are just manipulating specific data, sometimes using a global is okay, especially if you’re using it as read-only. (A lot of the confusing problems with globals arise when you need to mutate them or rebind them.) There are plenty of respectable libraries that have global variables storing various sorts of global parameters, such as customization options loaded from config files, etc.

    For a more heavyweight situation, if your task were more complex, you could create a class that basically does this:

    class Convert(object):
        def __init__(self):
            self.A = packageA.get_the_entirety_of_A_expensively()
            self.B = getB() # or put get_B's code in here
        def AB(self, input):
            return self.A[self.B[input]]
    
    converter = Convert()
    converter.AB('blah')
    

    This could be overkill if you just need to do one thing, but it’s the way to go if you need to parameterize the operation (e.g., if you had multiple A and B dicts that you might need to operate on).

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

Sidebar

Related Questions

My problem is a little bit complex. I have the following code: $(document).ready(function() {
My problem is a bit more complex than using the following simple JavaScript code:
Okay so this is a bit complex but I need to loop through this
I'm looking at a complex bit of JavaScript, which is doing all sorts of
Suppose I have a simple interface representing a complex number, whose instances would be
I have a complex problem and I am not sure what is causing it.
This is a bit complex so I hope I word this right. I have
A simple problem. I have an ASHX handler which generates a report. Unfortunately, this
Can anybody help me with this? Here's the problem... When I have to code
I have an xml file which looks like the below example <Ids> <Id> <set>ai</set>

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.