---------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基础编程243 ● 模块 ● 使用as给模块取别名】

 

 

 

 

 

 

 

【往期精彩】

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

 

【加群交流】

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

相关文章:

  • 2021-07-08
  • 2021-10-20
  • 2022-12-23
  • 2021-12-18
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-12
  • 2022-02-05
  • 2021-12-29
  • 2022-12-23
  • 2021-08-24
  • 2021-11-14
  • 2022-02-26
相关资源
相似解决方案