[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] How to export a function to a module or script



On Tue, May 28, 2002 at 07:25:34PM -0700, Pete Shinners wrote:
> Leonardo Santagada wrote:
> >	It isn't a problem directly related to pygame, but I'm making
> > a game and I was having this problem. I need to call a script from my
> >main program and I want it to use some function from the main one. What
> > I think I need is some way to export some functions to a python script
> > that I call from the main program. Does someone knows how to do that?
> 
> if i'm understanding this is just using the python "import".
> you can import any python file and access all of its globals..
> 
> import mymodule
> mymodule.myfunction()
> 
> if you are talking about a secondary module calling functions that are 
> available in the main module things get a little trickier. this is a 
> 'circular import'. you'll probably need to move the "common" functions from 
> the main module into a third module. sometimes this isn't easy to do. 
> sometimes the only way around tricky circular imports is to redesign things 
> a little.


Or you can export these functions to the module. For example,

import modules.module1

class Main:
  def init_mods (self):
    modules.module1.init_me (self)


then you can call Main's methods from the module. In the same way
you can pass references to functions

Another way is registering functions/objects/methods/classes in some dictionary
which is then imported by modules.

Jarda Benkovsky

-- 
GCM/IT d- s:+ a- C++(+++) ULOI++++$ P++ L+++>++++ E+ W++
N w--- PS+ PE++ Y+ PGP R+ tv- b+++ D+ e+++ y+

____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org