python基礎學習(十四)

28.模塊當腳本執行app

   !!!! 注意  這是分兩個文件的  一個是student.py和app3.pyspa

student.py3d

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結果:code

 

如今在student.py添加blog

print(name)
print(name_list)

run結果:it

 

 再打開app3.py輸入class

import student

app3.py 的 run結果import

 

 

爲了student.py能夠運行出來,app.py輸出爲空能夠在student.py 加以下:im

if (__name__ == "__main__"):

再次運行student.py:d3

 

 而後運行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__

相關文章
相關標籤/搜索