0

I have this module that calls main() function:

## This is mymodules ##
 def restart():
 r = input('Do you want to build another configuration file?\n1. Yes\n2. No\n')
 if r == '1':
 main()
 elif r == '2':
 os.system('pause')

The main() is in another script that loads this module. However when it calls it says main() is not defined. essentially this is what I have in my test:

import mymodules as my
def main():
 print('good')
my.restart()

When this runs I want the my.restart() to be able to call the main() defined.

asked Oct 12, 2016 at 17:24
1

1 Answer 1

2

For a code as simple as this one you could simply pass the main function as an argument to the restart function.

E.g.

def restart(function):
 r = input('Do you want to build another configuration file?\n1. Yes\n2. No\n')
 if r == '1':
 function()
 elif r == '2':
 os.system('pause')

And:

import mymodules as my
def main():
 print('good')
my.restart(main)

This is a popular design pattern known as a callback

However, this only works in simple examples like this. If you are writing something more complex you probably want to use objects and pass the objects instead. That way you will be able to call all multiple methods/functions from a single object.

answered Oct 12, 2016 at 17:39
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.