1

I've found resources on here but they're pertaining to locally embedded functions. I have one file called "test" and another called "main". I want test to contain all of my logic while main will contain a complete list of functions which each correlate with a health insurance policy. There are hundreds of policies so it would become quite tedious to write an if statement in "test" for each one each time. I'd like to write as few lines as possible to call a function based off of what a value states. Something like:

insurance = input()

The end result would not be an input but for testing/learning purposes it is. The input would always correlate with an insurance policy exactly if it exists. So on "tests" I currently have:

from inspolicy.main import bcbs, uhc, medicare

print('What is the insurance?(bcbs, uhc, medicare)')
insurance = input()

if insurance.lower() == 'bcbs':
    bcbs()
elif insurance.lower() == 'uhc':
    uhc()
elif insurance.lower() == 'medicare':
    medicare()
else:
    print('This policy can not be found in the database. Please set aside.')

With "main" including:

def bcbs():
    print('This is BCBS')

def uhc():
    print('This is UHC')

def medicare():
    print('This is Medicare')

So is there a way to have the input (i.e. insurance) be what is referenced against to call the function from "main"?

Thank you in advance for your time!

2 Answers 2

1

The best approach to this is to use a dictionary to map between the name of your insurance policies and the function that deals with them. This could be a hand-built dict in one of your modules, or you could simply use the namespace of the main module (which is implemented using a dictionary):

# in test
import types
import inspolicy.main # import the module itself, rather than just the functions

insurance = input('What is the insurance?(bcbs, uhc, medicare)')

func = getattr(inspolicy.main, insurance, None)
if isinstance(func, types.FunctionType):
    func()
else:
    print('This policy can not be found in the database. Please set aside.')
Sign up to request clarification or add additional context in comments.

1 Comment

You're the real MVP! Thank you so much for your help. Admittedly I'm beginner for sure but I always like learning the more complicated stuff because it helps me understand the other parts more in the long run. Thank you so much!
0

Let's consider this is your main.py

def uhc():
    print("This is UHC")

It is possible to do something like that in test.py:

import main

def unknown_function():
    print('This policy can not be found in the database. Please set aside.')

insurance = input()

try:
    insurance_function = getattr(main, insurance.lower())
except AttributeError:
    insurance_function = unknown_function

insurance_function()

Then, if you type "uhc" as your input, you will get the uhc function from main.py and call it.

1 Comment

Thank you so much for getting back to me so quick!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.