By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Using a function from __main__ from within an imported module

P: 12
Hello all,
I was hoping that someone could help me out with a python programming question.
I have a main program that imports a module which consists of a class. What I cannot seem to do is get a function of this class to direct the program to a function in the main program.

Basically:

What is in the module:
Expand|Select|Wrap|Line Numbers
  1. class():
  2.     def random_function():
  3.         call_function_from_main()
And when this is imported it says that the call_function_from_main() is not a locally defined. I understand why this is, but am asking if there is a work-around so that I can call the function.

I am sorry about the vague code and the difficulty in expressing exactly what I mean, but the actual code is far too long to post (people would get bored with it!)

One thing is that I am resistant to simply moving the code for the class into the main programs code (one possible solution), because I would have to restructure a considerable amount of my other modules code to do so.

If anyone can help it would be greatly appreciated. If there is a need for further clarification I'll be checking back periodically.
Thank you in advance for your time, this place has always been a great help to me.
Mar 25 '08 #1
Share this Question
Share on Google+
4 Replies


jlm699
100+
P: 314
For situations like this a good thing to do is to have a self object for a class. Refer to Python Docs for more info, but basically you just need to pass the 'other class' an instance where it can reach the scope of the function you wish to call.

Something like
Expand|Select|Wrap|Line Numbers
  1. def randomfunc(self, parent, input):
  2.     self.parent = parent
  3.     ret = parent.func_to_call()
  4.     print ret
  5.  
Mar 25 '08 #2

P: 12
Thank you very much for responding.
The problem with that solution for me is that the __init__ of the main class has a value that is an instance of the imported class. Here is the simplified troublesome part of the code:

Expand|Select|Wrap|Line Numbers
  1. class Main(object):
  2.  
  3.     def __init__(self):
  4.  
  5.         root = Tk()
  6.         root.title = "Spider Solitaire"
  7.         self.set_up_deck()
  8.         self.load_card_images()
  9.         self.tableau = Tableau()  
  10.         self.deal()
  11.         self.stock = Stock(self.deck)
  12.         frame = TableauFrame(self.tableau, root)
  13.         frame.pack()
  14.         mainloop()
What I need to do is pass self.stock into Tableau. But self.stock depends on Tableau already existing, i.e. without Tableau, I cannot make self.stock. Because of this I can't pass it to Tableau (or maybe I can? If I can that would be great!) because Tableau exists before self.stock
Mar 25 '08 #3

P: 12
Ahh ok I've got it.
I just added a function to the tableau class that can be called by main to pass the self.stock to to the tableau after it's created.

Again an embarrassingly simple solution.
Sorry for the trouble and thank you for the time.
Mar 25 '08 #4

jlm699
100+
P: 314
What I need to do is pass self.stock into Tableau. But self.stock depends on Tableau already existing, i.e. without Tableau, I cannot make self.stock. Because of this I can't pass it to Tableau (or maybe I can? If I can that would be great!) because Tableau exists before self.stock
It's hard for me to understand why that is? Are these functions that you have created? Can you try passing self.stock = None to Tableau?
If you created these functions, why did you create them that way? If not... where are those functions inherited from?
Mar 25 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.