Module's Custom module

Source: Internet
Author: User

Make your own module
Creating your own module is very simple and you've been doing it! Each Python program is also a module. You have ensured that it has a. py extension. The following example will make it clearer.
Create your own module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example 8.3 How to create your own module

# !/usr/bin/python # Filename:mymodule.py def Sayhi ():     Print ' Hi, this is MyModule speaking ' version='0.1'#End of mymodule.py

Above is an example of a module. As you've seen, it's nothing special compared to our normal Python program. We'll look at how to use this module in our other Python programs.
Remember that this module should be placed in the same directory as the program we entered it, or in one of the directories listed in Sys.path.

# !/usr/bin/python # Filename:mymodule_demo.py Import Mymodulemymodule.sayhi () Print ' version ', mymodule.version

Output
$ python mymodule_demo.py
Hi, this is MyModule speaking.
Version 0.1
How it works
Note that we use the same dot number to use the members of the module. Python is good at reusing the same tokens, so that we python programmers don't need to learn new methods constantly.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From.. Import
Here is a use from: The version of the import syntax.

# !/usr/bin/python # Filename:mymodule_demo2.py  from Import Sayhi,versionsayhi () Print ' Version ', version
The output of the mymodule_demo2.py is exactly the same as the mymodule_demo.py.

Module's Custom module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.