[問題] __init__.py

看板Python作者 (平凡)時間7年前 (2016/11/12 11:24), 7年前編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
今天看到套件章節 有一個地方不是很懂, 有人可以解惑嗎?謝謝 ---------------------------------- \test.py from source import test1,test2 print(test1.get_list()) print(test2.get_list()) ---------------------------------- source\test1.py def get_list(): list = [1,2,3] return (list) ---------------------------------- source\test2.py def get_list(): list = [4,5,6] return (list) ---------------------------------- 在命令提示字元 C:\Python3>python test.py [1, 2, 3] [4, 5, 6] 問題: 書中提到 "須要在source目錄中放一個 __init__.py的檔案 它可以是空的,python才可以將容納的目錄視為套件" 但是, 建與沒建, 都可以得到正確的結果, 我看不出 有它/沒它 的差別..... 有人知道嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.25.181.127 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1478921069.A.698.html

11/12 12:59, , 1F
沒跳 cannot import name 'source' 嗎
11/12 12:59, 1F

11/12 14:42, , 2F
要把 __pycache__ 砍掉才看得出差異,不然會 cache
11/12 14:42, 2F

11/12 17:23, , 3F
資料夾整個?還是裡面__init__.cpython-35
11/12 17:23, 3F
※ 編輯: GooLoo (114.25.181.127), 11/12/2016 17:34:54

11/12 20:43, , 4F
理論上只要 __init__.cpython-35 就好, 但都砍掉也沒差
11/12 20:43, 4F
文章代碼(AID): #1O9ejjQO (Python)