第5章 python模組使用

來源:互聯網
上載者:User

標籤:python

#匯入模組幹了哪些事:

#1 執行源檔案

#2 以一個源檔案的全域名稱空間

#3 在當前位置拿到一個模組名,指向2建立的名稱空間




# import spam

# money=100000000000

# def read1():

#     print(‘from test‘)

# # print(spam.money)

# # print(spam.read1)

# # spam.read1()

#

# # spam.read2()

# spam.change()

# print(money)

# spam.read1()



#

# import spam as s1

# print(s1.money)





# sql_type=input(‘sql_type: ‘)

# if sql_type == ‘mysql‘:

#     import mysql as sql

#

# elif sql_type == ‘oracle‘:

#     import oracle as sql

#

# sql.sqlparse()





import sys

print(sys)

import spam

print(spam)




#優點:使用源檔案內的名字時無需加首碼,使用方便

#缺點:容易與當前檔案的名稱空間內的名字混淆

# from spam import money,read1,read2,change

# money=0

# print(money)

# print(read1)

#

# read1()


# def read1():print(‘ok‘)

# read2()


#

# money=10

# change()

# print(money)



# from spam import money as m

#

# print(m)





from spam import *


# print(_money)

# read1()

# print(read2)


print(money)

print(x)

print(read1)




#模組只在第一次匯入時才會執行,之後的匯入都是直接引用記憶體已經存在的結果

import sys

print(‘spam‘ in sys.modules) #存放的是已經載入到內的模組



import spam

print(‘spam‘ in sys.modules)

# import spam

# import spam

# import spam

# import spam

# import spam


第5章 python模組使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.