28.模块当脚本执行
!!!! 注意 这是分两个文件的 一个是student.py和app3.py
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}')
run结果:
现在在student.py添加
print(name) print(name_list)
run结果:
再打开app3.py输入
import student
app3.py 的 run结果
为了student.py可以运行出来,app.py输出为空可以在student.py 加如下:
if (__name__ == "__main__"):
再次运行student.py:
然后运行app3.py
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
app3.py输入
import student print(dir()) print(dir(student)) print(__name__) print(student.__name__)
run结果:两个都有__name__ 然后打印输出__name__ 结果为 app3.py这个是__main__ ||||| student.py 是 student
再回到student.py 输入
print(__name__)
run结果:这次student就是 __main__