26.模块
第一个student.py
name = "Song Ke" name_list = ["张三", "李四", "王五"] def who_am_i(myName): print(f'my name is : {myName}') class Student: def __init__(self, name, age, sex): self.__name = name self.__age = age self.__sex = sex def who_am_i(self): print(f'i am a student , {self.__name}, {self.__age}, {self.__sex}')
第二个app.py
# 模块 # PYTHONPATH import student print(student.name) print(student.name_list) student.who_am_i("yang") import sys print("搜索路径-------") for line in sys.path: print(line)
run结果:
27.模块2.0
test1:会报错
import student # Traceback (most recent call last): # File "/home/yangbin/Study/IdeaProjects/pythonWork/day4/module/app1.py", line 2, in <module> # sk = Student("sk", 21, "男") # NameError: name 'Student' is not defined sk = Student("sk", 21, "男") sk.who_am_i() print("-----------------1------------------------") # 同名会bb不会替换yang name = "bb" # import 另外导入形式 * 导出所有属性 from student import Student,name,name_list,who_am_i # 这个cc会替换 name = "Song Ke" name = "cc" skk = Student("skk", 22, "男") skk.who_am_i() print("-------------------2-------------------") print(name) who_am_i("yang") print("--------------------3--------------------") # 别名 import student as s sk = s.Student("sk1", 21, "男") sk.who_am_i() # dir 查看所有属性 print(dir(s))
test2:
import student # Traceback (most recent call last): # File "/home/yangbin/Study/IdeaProjects/pythonWork/day4/module/app1.py", line 2, in <module> # sk = Student("sk", 21, "男") # NameError: name 'Student' is not defined sk = student.Student("sk", 21, "男") sk.who_am_i() print("-----------------1------------------------") # 同名会bb不会替换yang name = "bb" # import 另外导入形式 * 导出所有属性 from student import Student,name,name_list,who_am_i # 这个cc会替换 name = "Song Ke" name = "cc" skk = Student("skk", 22, "男") skk.who_am_i() print("-------------------2-------------------") print(name) who_am_i("yang") print("--------------------3--------------------") # 别名 import student as s sk = s.Student("sk1", 21, "男") sk.who_am_i() # dir 查看所有属性 print(dir(s))
run结果: