Use of Python modules

Source: Internet
Author: User

I. Standard template using built-in module SYS

#hello. py file

#!/usr/bin/env Python3
#-*-Coding:utf-8-*-#本文件使用utf-8 encoding


' A test module ' #任何模块代码的第一个字符串都被视为模块的文档注释, does not print output


__author__ = ' Michael Liao ' #作者名


Import SYS #导入模块sys

def test ():
args = sys.argv #SYS.ARGV is a list that stores a string of command-line input

Print (args)


If Len (args) ==1:
Print (' Hello, world! ')
Elif len (args) ==2:
Print (' Hello,%s! '% args[1])
Else
Print (' Too many arguments! ')


If __name__== ' __main__ ': #函数入口, the direct runtime executes the statement under T, and when used as a module, does not output the following statement;
Test ()


Second, call the Hello module

1. The modulation module does not use if __name__== ' __main__ ':

A. New get.py file Call hello.py

#get. py

#!/usr/bin/env Python3
#-*-Coding:utf-8-*-

' A test module '

__author__ = ' Sume ' #作者名

From Hello Import Test #导入模块hello


Def get ():
Print (' Lalala ')


Get ()
Test ()


b.hello.py file

#-*-Coding:utf-8-*-

' A test module '

__author__ = ' Michael Liao ' #作者名

Print (' The first print ') #添加此处print

Import SYS #导入模块sys
def test ():
args = sys.argv #sys. argv is a list that stores the string entered by the command line
Print (args)
If Len (args) ==1:
Print (' Hello, world! ')
Elif len (args) ==2:
Print (' Hello,%s! '% args[1])
Else
Print (' Too many arguments! ')


Test ()



2. The modulated module uses the if __name__== ' __main__ ':  

a.get.py file


#!/usr/bin/env Python3
#-*-Coding:utf-8-*-

' A test module '

__author__ = ' Sume ' #作者名

From Hello Import Test #导入模块hello


Def get ():
Print (' Lalala ')


Get ()
Test ()


b.hello.py file

#-*-Coding:utf-8-*-

' A test module '

__author__ = ' Michael Liao '

Print (' The first print ') #添加此处print

import sys  #导入模块sys    
def test (): 
    args = sys.argv   < br>    print (args)
    if Len (args) ==1:
         Print (' Hello, world! ')
    elif len (args) ==2:
        print (' Hello,%s! '% args[1]
    Else:
        print (' Too many arguments! ')


if __name__== ' __main__ ':   # add here

Test ()


Use of Python modules

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.