python import xxx 與 from xxx import xx 模組引入的區別

來源:互聯網
上載者:User

標籤:pytho   區別   .com   引入   rip   http   bbb   ges   方式   

 

有如下指令碼script1.py:

A=‘aaaa‘
B=‘bbbb‘
C=‘cccc‘
print A,B,C

 

1.命令列互動模式下使用import 匯入方式1:

>>>import script1

再分別輸入命令列印:

>>>print script1.A, script1.B, script1.C

>>>print A,B,C

結果如下:

可以看到

 print script1.A, script1.B, script1.C正常取到A,B,C的值

但是print A,B,C卻報錯了,A未聲明

2.使用from ... import ...的方式匯入:

>>>from script1 import A,B,C

分別輸入命令列印:

>>>print script1.A, script1.B, script1.C

>>>print A,B,C

其運行結果如下:

 

可以看到,print A,B,C正確取到值,但是script1.A 報錯,script1未聲明

綜上可以看出 兩種方式引入的區別,這兩種方式匯入的變數儲存是這樣的:

 

 

python import xxx 與 from xxx import xx 模組引入的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.