28.模塊當腳本執行 !!!! 註意 這是分兩個文件的 一個是student.py和app3.py student.py name = "Song Ke" name_list = ["張三", "李四", "王五"] def who_am_i(myName): print(f'my name is ...
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__