from与import之间的区别
来源:互联网 发布:淘宝评价截图 编辑:程序博客网 时间:2024/05/13 22:07
实例:
模块foo.py的内容:
yuqiu@w1-01-11-0492:~$ cat foo.py
#!/usr/bin/python
# Filename: foo.py
bar='hello'
print bar
先用from导入模块:
>>> from foo import bar
hello #说明模块已经被执行了一次,from和import都会对模块里的语句执行一遍
>>> bar
'hello' #从模块里导入的内容
>>> bar='yuqiu' #对变量重新赋值
>>> foo.bar
'hello' #为什么boo.bar的值不是‘yuqiu’呢?
原因:
from导入模块的过程:
1、寻找模块foo的位置;
2、将foo源代码编译成字节码(根据需要)
3、执行字节码
执行字节码的过程
foo.bar='hello'
bar=foo.bar #此时‘hello’字符串对象便有两个引用计数
delete foo
对bar重新赋值后bar='yuqiu',bar会引用到另外一个字符串对象'yuqiu'
import foo #再次导入模块
foo.bar会从内存中读取,所以foo.bar的值为'hello'
模块foo.py的内容:
yuqiu@w1-01-11-0492:~$ cat foo.py
#!/usr/bin/python
# Filename: foo.py
bar='hello'
print bar
先用from导入模块:
>>> from foo import bar
hello #说明模块已经被执行了一次,from和import都会对模块里的语句执行一遍
>>> bar
'hello' #从模块里导入的内容
>>> bar='yuqiu' #对变量重新赋值
再用import导入模块:
>>> foo.bar
'hello' #为什么boo.bar的值不是‘yuqiu’呢?
原因:
from导入模块的过程:
1、寻找模块foo的位置;
2、将foo源代码编译成字节码(根据需要)
3、执行字节码
执行字节码的过程
foo.bar='hello'
bar=foo.bar #此时‘hello’字符串对象便有两个引用计数
delete foo
对bar重新赋值后bar='yuqiu',bar会引用到另外一个字符串对象'yuqiu'
import foo #再次导入模块
foo.bar会从内存中读取,所以foo.bar的值为'hello'
- from与import之间的区别
- python中import与from import之间的区别
- python import与from...import....的区别
- from import 与 import 的区别
- import 与import ... from 区别
- Python之import与from...import的区别与用法
- Python中from...import与import......as的区别
- python中模块导入语句 from ..import ..与from import as的区别
- from module import 和 import 的区别
- from module import 和 import 的区别
- 零基础学python-import与from的区别
- @import 与 #import 的区别
- import与@import的区别
- Python中import, from...import,import...as的区别
- import与from...import....
- from.....import 与 import
- import与from...import....
- Python基础 - from import方法与import方法的区别
- Storyboard全解析-第二部分
- MFC 窗口分割与通信
- Magento 常用技巧与方法
- Selenium测试Ajax程序
- 软件产品和软件项目
- from与import之间的区别
- 字符集合问题
- C#项目打开/保存文件夹/指定类型文件,获取路径
- hibernate.hbm2ddl.auto配置详解
- List of all Oracle Server Parameters
- android 登录前检查网络状态
- 指针(作者girlrong)
- apk文件反编译以及签名打包
- java.util.ConcurrentModificationException 的解决方法