【Python基础编程243 ● 模块 ● 使用as给模块取别名】


 ---------Python基础编程---------

Author : AI菌


【内容讲解】

# import module01 as m01
# import module02 as m02
或者
# import module01 as m01, module02 as m02

【代码演示】

# import module01
# import module02

# import module01 as m01
# import module02 as m02

import module01 as m01, module02 as m02

print(m01.a)
print(m01.func1(5, 6))
s1 = m01.Student("robot", 20)
print(s1)

print("----------")

print(m02.a)
print(m02.func1(5, 6))
s2 = m02.Student("rabbit", 19)
print(s2)

module01:

# 定义全局变量
a = 100


# 定义函数
def func1(a, b):
    return a + b


# 定义类
class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"name={self.name},age={self.age}"

module02:

# 定义全局变量
a = 200


# 定义函数
def func1(a, b):
    return a - b


# 定义类
class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"name={self.name},age={self.age}"

【运行结果】

【往期精彩】

▷【Python基础编程196 ● 读取文件的4种方式】
▷【Python基础编程197 ● 读取文件的4种方式】
▷【Python基础编程198 ● 读取文件的4种方式】
▷【Python基础编程199 ● Python怎么读/写很大的文件】
▷【Python基础编程200 ● 读取文件的4种方式】
▷【Python基础编程201 ● 读取文件的4种方式】
▷【Python基础编程202 ● 读取文件的4种方式】
▷【Python基础编程203 ● 读取文件的4种方式】

【加群交流】



原文地址:https://www.cnblogs.com/hezhiyao/p/13476710.html