Python中的 "import" 和 "from ... import"
2014-08-07 16:16
465 查看
在python中, 怎么选择使用"import" 或者"from .. import" 呢
原则就是: 无论使用哪种import方式, 都必须满足
只import module, 不import module中的对象
让后面的代码可以直接使用不包含package信息的module name.
Bad (没import module, 而是import了module中的server函数)
from eventlet.wsgi import server
server(eventlet.listen(('',8080)), hello_world)
Bad (后面的代码需要使用wsgi这个module的时候, 还需要同时使用package信息“eventlet.wsgi”)
import eventlet.wsgi
eventlet.wsgi.server(eventlet.listen(('',8080)), hello_world)
Good
from eventlet import wsgi
wsgi.server(eventlet.listen(('',8080)), hello_world)
import netaddr
meta_cidr = netaddr.IPNetwork(dhcp.METADATA_DEFAULT_CIDR)
from neutron.agent.common import config
self.root_helper = config.get_root_helper(self.conf)
原则就是: 无论使用哪种import方式, 都必须满足
只import module, 不import module中的对象
让后面的代码可以直接使用不包含package信息的module name.
Bad (没import module, 而是import了module中的server函数)
from eventlet.wsgi import server
server(eventlet.listen(('',8080)), hello_world)
Bad (后面的代码需要使用wsgi这个module的时候, 还需要同时使用package信息“eventlet.wsgi”)
import eventlet.wsgi
eventlet.wsgi.server(eventlet.listen(('',8080)), hello_world)
Good
from eventlet import wsgi
wsgi.server(eventlet.listen(('',8080)), hello_world)
import netaddr
meta_cidr = netaddr.IPNetwork(dhcp.METADATA_DEFAULT_CIDR)
from neutron.agent.common import config
self.root_helper = config.get_root_helper(self.conf)
相关文章推荐
- Python报错"undefined variable from import..."
- System commands can run from "cmd"
- directshow的"hello world"(from sdk sample)
- suse 11下编译使用boost-python报错:error: changes meaning of 'keywords' from 'struct boost::python::detail::keywords'
- How to eliminate the compile error "conversion from 'double ' to 'float ', possible loss of data"
- Preventing "Return" and "Esc" From Closing Dialog
- strSQL = "Select * From " & strTableName & " Where False "
- [原创][Python 技术培训] 第一周幻灯片 "Python 基础篇"
- Service部署到服务器后出现"The test form is only available for requests from the local machine"解决方法
- It's time to retire "ready for the desktop"――from linux.com
- null, message from server: "Host ***" is not allowed to connect to this MySQL server"
- 关于如select 1 from **中的"1"。
- Notes for "Next.Generation.Application.Integration.From.Simple.Information.To.Web.Services"
- JVM - Concept and Lifetime (From book "Inside the Java Virtual Machine")
- [ZT]Understanding Python's "with" statement
- .NET数据访问策略:From "LINQ to SQL" to "LINQ to X"
- [Python]用Python编写程序修复"恶邮差"变种U病毒感染文件。
- 找出包含字符串"from"但不含字符串"127.0.0.1"的txt文件 (用SQL+Batch+grep求两个文件集合的差.)
- sco unix No utmp entry. You must exec "LOGIN" from the lowest level "sh"
- 内存变量 Export / Import ...To Memory ID 'ID_Name' / From Memory ID 'ID_Name' 导出/读取的使用