次の方法で共有


Learning IronPython #6: Creating and Working with Modules

Section 6 of the Python Tutorial has this to say about Python modules: "If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you've written in several programs without copying its definition into each program. To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules...A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended..."

So, if you create a module named HelloWorld.py with the following contents:

# A pound symbol designates a comment to the end of the current line.
# The def keyword defines a function.
# The print function prints to the console.
def HelloWorld():
print "Hello, World!"

And you save HelloWorld.py to a folder such as c:\IronPython\MyModules, you can use the module's functionality as follows:

>>> # Add c:\IronPython\MyModules to IronPython's module search path.
... # Use "r" to designate a literal string.
... sys.path.append(r"c:\IronPython\MyModules")
>>> # Confirm the path is correct, then import the HelloWorld.py
... # module, and call the HelloWorld function.
... sys.path
[... (Omitted for brevity) ... 'c:\\IronPython\\MyModules']
>>> import HelloWorld
>>> HelloWorld.HelloWorld()
Hello, World!

You can import several modules at once. For example, if you create a module named GoodbyeConsole.py with the following contents:

def SeeYouLater():
print "Goodbye!"

And then you type the following in the console:

>>> import GoodbyeConsole

You could then type the following in the console:

>>> dir(HelloWorld)
['HelloWorld' ... (omitted for brevity) ...]
>>> dir(GoodbyeConsole)
['SeeYouLater' ... (omitted for brevity) ...]
>>> HelloWorld.HelloWorld()
Hello, World!
>>> GoodbyeConsole.SeeYouLater()
Goodbye!

You can even chain modules together. For example, if you create a module named HelloGoodbye.py with the following contents:

import HelloWorld
import GoodbyeConsole

def TheEnd():
print "That's all, folks!"

You could then type the following in the console:

>>> import HelloGoodbye
>>> dir(HelloGoodbye)
['GoodbyeConsole', 'HelloWorld', 'TheEnd' ... (omitted for brevity) ...]
>>> dir(HelloGoodbye.GoodbyeConsole)
['SeeYouLater' ... (omitted for brevity) ...]
>>> dir(HelloGoodbye.HelloWorld)
['HelloWorld' ... (omitted for brevity) ...]
>>> # HelloWorld is now part of the HelloGoodbye namespace.
... HelloGoodbye.HelloWorld.HelloWorld()
Hello, World!
>>> # GoodbyeConsole is now part of the HelloGoodbye namespace.
... HelloGoodbye.GoodbyeConsole.SeeYouLater()
Goodbye!
>>> HelloGoodbye.TheEnd()
That's all, folks!

-- Paul

------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.

Comments

  • Anonymous
    May 30, 2006
    To build upon my last post, you can run a script file by calling the execfile function and pass in the...
  • Anonymous
    March 20, 2009
    To build upon my last post , you can run a script file by calling the execfile function and pass in the