python中将函数存储在模块里(使用as给函数指定别名)

1、将三个函数fun1\fun2\fun3存储在名称为module1.py的模块中

def fun1(x):
    print(x.upper())

def fun2(x):
    print(x.title())

def fun3(x):
    print(x)
    

2、给函数fun1指定别名abc.

>>> abc("aaaa")    ## 不可以调用
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    abc("aaaa")
NameError: name 'abc' is not defined
>>> from module1 import fun1 as abc   ## 导入方法  在导入函数的方法的基础上增加 as 别名
>>> abc("aaaa")
AAAA

3、为函数fun2指定别名为bcd.

>>> bcd("aaaa")           ## 不可以调用
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    bcd("aaaa")
NameError: name 'bcd' is not defined
>>> from module1 import fun2 as bcd
>>> bcd("aaaa")           ## 可以调用
Aaaa
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14525934.html