[Tutor] another ?-->if I created test.py how do I run test.py in hello.py and also how do I run it again in hello.py?

Doug Stanfield DOUGS@oceanic.com
Wed, 23 Aug 2000 11:23:04 -1000


This one is one of the great features of Python, the modularity.

Usually you want to structure your programs in small lumps of functionality
in modules.  Some people suggest on the order of one class per file, or
module.  I think that is extreme and usually have up to ten classes or
related functions in one module.  The structure might look like this:

#-------file:  mymodule.py
import spam,eggs		# whatever library modules are needed.

def afunction(var1):
	# some good code

def another(var2):
	# more useful stuff

#------ might be more functions or classes

def test():
	# something which tests this module

def main():
	# I'd use a main if this is a module that is expected to be
	# run as an application
	outstuff = afunction(theInput)
	another(outstuff)	# making it up for the example

if __name__ == '__main__':
	main()	# This is if its an application, not a library of functions

#----------end of file

Lets say that the function 'another' is good stuff that you want to use in
another of your programs.  You would do the following in the new file:

import mymodule

# and then to run it:

mymodule.another(theInput)

#-----------------

This is the same semantics as is used to call all the Python libraries
functions so it should be familiar.

Hope this is what you were asking.

-Doug-



> -----Original Message-----
> From: Charles Gruschow, Jr. [mailto:c.gruschow@prodigy.net]
> Sent: Wednesday, August 23, 2000 10:58 AM
> To: Daniel Yoo
> Cc: tutor@python.org
> Subject: Re: [Tutor] another ?-->if I created test.py how do I run
> test.pyin hello.py and also how do I run it again in hello.py?
> 
> 
>  I just thought that if you have a block of code that works 
> on its own, you
> could use that block in a different program if you wanted.  I 
> wanted to know
> how to originally call that block of code, and how to use it 
> again in the
> same program if you wanted to.  Like for example if you 
> created an option
> off a pull down menu that executes a python program, how do 
> you get it so
> that you can execute that option again.
> 
> BTW, thank you very much for your assistance so far.
> 
> Thanks,
> 
> Charles W. Gruschow, Jr.
> c.gruschow@prodigy.net
> 
> p.s. I am learning quite a bit considering I only just got Python last
> Friday.  :)
> 
> 
> 
> 
> ----- Original Message -----
> From: Daniel Yoo <dyoo@hkn.EECS.Berkeley.EDU>
> To: Charles Gruschow, Jr. <c.gruschow@prodigy.net>
> Cc: <tutor@python.org>
> Sent: Wednesday, August 23, 2000 2:31 PM
> Subject: Re: [Tutor] another ?-->if I created test.py how do 
> I run test.pyin
> hello.py and also how do I run it again in hello.py?
> 
> 
> > On Wed, 23 Aug 2000, Charles Gruschow, Jr. wrote:
> >
> > > another ?-->if I created test.py how do I run test.py in 
> hello.py and
> > > also how do I run it again in hello.py?
> >
> >
> > I'm not quite sure if this is what you mean, but you can use
> >
> >     execfile("test.py")
> >
> > inside your hello.py file, and that'll execute test.py for 
> you.  However,
> > this is something uncommon to me.  Can you explain to us 
> why you want to
> > do this?
> >
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>