469,332 Members | 6,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

Using a function from __main__ from within an imported module

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
4 1166
jlm699
314 100+
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
Traclo
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
Traclo
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
314 100+
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.

Similar topics

reply views Thread by Uwe Mayer | last post: by
2 posts views Thread by Ron_Adam | last post: by
1 post views Thread by jmalone | last post: by
9 posts views Thread by jezonthenet | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.