python函数模块化

函数模块化

1.python模块可以在逻辑上组织Python程序,将相关的程序组织到一个模块中去,使用良好的结构化设计,增加程序的重用性,提高代码使用率

例子 输入mytest.py

def print_mytest(name):

      return ("mytest=",name)

 

再创建一个文件为mytest2.py mytest2 导入mytest.py

import mytest

mytest.print_mytest("mytest")

运行结果为

mytest=mytest

可以看出mytest2 直接调用了mytest里面的print_mytest方法

 

2.导入特定方法

例子为;

from module_name import function_name

也可以导入多个

from module_name import function_name1,function_name2,function_name3

3.函数别名

格式为:

from module_name import function_name as fn

例如:

import numpy as np

4.内置函数

 

python函数模块化