[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?
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:
import spam,eggs # whatever library modules are needed.
# some good code
# more useful stuff
#------ might be more functions or classes
# something which tests this module
# 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:
# and then to run it:
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.
> -----Original Message-----
> From: Charles Gruschow, Jr. [mailto:firstname.lastname@example.org]
> Sent: Wednesday, August 23, 2000 10:58 AM
> To: Daniel Yoo
> Cc: email@example.com
> 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.
> Charles W. Gruschow, Jr.
> 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. <firstname.lastname@example.org>
> Cc: <email@example.com>
> 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