模块和包

#my_module.py
print('from the my_module.py')

money=1000

def read1():
    print('my_module->read1->money',money)

def read2():
    print('my_module->read2 calling read1')
    read1()

def change():
    global money
    money=0

 

1.模块和包,同级下使用import导入

import a #导入a.py

a.read2() #执行a.py里面read2方法,这里的a是名称空间

2.包里面的变量和自己当下模块的变量不会冲突,如果使用自己模块下的直接用,引用模块的,使用引用名.变量(方法)

3.使用别名 import my_module as module

if file_format == 'xml':
    import xmlreader as reader
elif file_format == 'cvs':
    import csvreader as reader

data = reader.read_date(filename)

 4. from my_module import read1  #从my_module中调用read1方法 ,如果当前模块有同名函数,那么就覆盖

 5. 支持多行导入: from my_module import (read1, read2, money)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值