Python论坛  - 讨论区

标题:[python-chinese] 这两个输入模块的方法哪个好?大家都用哪个

2007年12月17日 星期一 09:55

python st python.st在gmail.com
星期一 十二月 17 09:55:01 HKT 2007

*<1>.from Ä£¿éÃû import ±äÁ¿/º¯Êý*
**
*<2>.import Ä£¿éÃû*
**
*ÕâÁ½¸ö·½·¨µÄ²î±ðÔÚÄÄ£¿   ÎÒÔÚѧϰµÄʱºòÒ»°ã¶¼Ê¹ÓÃ"import Ä£¿éÃû"µÄ·½·¨£¬ÎÒ¾õµÃ
ÕâÑù×ö¿ÉÒÔ·ÀÖ¹³ÌÐòÖбäÁ¿Ãû»òÕߺ¯ÊýÃûÒòͬÃûÏà³åÍ»£¬µ«ÊǽñÌì¿´Á˼¸¸ö½éÉÜWEB¿ò¼ÜµÄÎÄÕÂÖУ¬´ó¶à¶¼Ê¹ÓÃÁ˵ÚÒ»ÖÖ·½·¨À´ÊäÈëÄ£¿é£¬ÇëÎÊ´ó¼ÒÒ»°ãÓõÄʱºòÓÃÄĸö·½·¨£¿
Ϊʲô£¿
*
-- 
PythonÐÂÈË£¬ÕýÔÚ¶ñ²¹PythonÓï·¨£¬Èç¹ûÎÊÌâÖк¬Óа׳յÄÏÖÏ󣬾´ÇëÁ½⡣
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071217/3d846683/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月17日 星期一 10:08

limodou limodou在gmail.com
星期一 十二月 17 10:08:21 HKT 2007

On Dec 17, 2007 9:55 AM, python st <python.st在gmail.com> wrote:
> <1>.from 模块名 import 变量/函数
>
> <2>.import 模块名
>
> 这两个方法的差别在哪?   我在学习的时候一般都使用"import 模块名"的方法,我觉得
> 这样做可以防止程序中变量名或者函数名因同名相冲突,但是今天看了几个介绍WEB框架的文章中,大多都使用了第一种方法来输入模块,请问大家一般用的时候用哪个方法?
> 为什么?
>
一般使用import 为好,而且还可以

import a as b

这样起一个很短的别名使用起来挺方便的。

而使用from a import
*这种往往是东西太多了,所以一次导入,只要你知道有哪些东西就行了。不过象语法检查可能就无法检查了,而使用import更方便语法检查。

-- 
I like python!
UliPad <>: http://code.google.com/p/ulipad/
meide <>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月17日 星期一 10:17

jeff jie bbmyth在gmail.com
星期一 十二月 17 10:17:08 HKT 2007

<1> from mod import fn ½ö½öÊǰÑModÄ£¿éÏÂÃæµÄfnº¯ÊýImport½øÀ´¡£Ê¹ÓÃʱֱ½Ófn(xxx)¾Í¿ÉÒÔÁË¡£
<2> import mod
ÔòÊǰÑÕû¸öÄ£¿é¶¼Import½øÀ´ÁË¡£´ÓÔËÐÐЧÂÊÉÏÀ´±È½Ï£¬<1>À´µÃÒªºÃÒ»µã¡£µ«Êǵ±Ò»¸öÄ£¿éÀïÒªImport½øºÜ¶àµÄ»°£¬Ê¹ÓÃÕâÖÖ·½·¨ÔÚ±à³Ì·½ÃæµÄЧÂÊÒªºÃµã¡£Ê¹ÓÃÕâÖÖ·½Ê½Import½øÀ´ºó£¬ÒªÍ¨¹ýÕâÑù£º
mod.fn(xxx)ȥʹÓÃÄ£¿éÀïµÄ×ÊÔ´¡£¶øÁíÒ»ÖÖÌæ´ú·½Ê½Îª£ºfrom mod import *
¡£Í¬ÑùÊÇImportÈ«²¿£¬²»¹ý¿ÉÒÔÏñ<1>ÄÇÑùʹÓᣵ«Óв»×ãµÄµØ·½¾ÍÊǵ±Á½¸öÄ£¿éÓÐÏàͬÃû³ÆµÄº¯Êý»òÀàµÄ»°ÕâÑùµÄµ¼È뷽ʽÈÝÒײúÉú³åÍ»¶øÄÑÒÔ·¢ÏÖ¡£
¸öÈËÓÞ¼û £ºP

ÔÚ07-12-17£¬python st <python.st在gmail.com> дµÀ£º
>
> *<1>.from Ä£¿éÃû import ±äÁ¿/º¯Êý*
> **
> *<2>.import Ä£¿éÃû*
> **
> *ÕâÁ½¸ö·½·¨µÄ²î±ðÔÚÄÄ£¿   ÎÒÔÚѧϰµÄʱºòÒ»°ã¶¼Ê¹ÓÃ"import Ä£¿éÃû"µÄ·½·¨£¬ÎÒ¾õµÃ
> ÕâÑù×ö¿ÉÒÔ·ÀÖ¹³ÌÐòÖбäÁ¿Ãû»òÕߺ¯ÊýÃûÒòͬÃûÏà³åÍ»£¬µ«ÊǽñÌì¿´Á˼¸¸ö½éÉÜWEB¿ò¼ÜµÄÎÄÕÂÖУ¬´ó¶à¶¼Ê¹ÓÃÁ˵ÚÒ»ÖÖ·½·¨À´ÊäÈëÄ£¿é£¬ÇëÎÊ´ó¼ÒÒ»°ãÓõÄʱºòÓÃÄĸö·½·¨£¿
> Ϊʲô£¿
> *
> --
> PythonÐÂÈË£¬ÕýÔÚ¶ñ²¹PythonÓï·¨£¬Èç¹ûÎÊÌâÖк¬Óа׳յÄÏÖÏ󣬾´ÇëÁ½⡣
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
site:http://www.fallever.com
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071217/52e68495/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月17日 星期一 11:46

jessinio liang jessinio在gmail.com
星期一 十二月 17 11:46:03 HKT 2007

书上说,使用from ... import ...一般是加入包中的模块到程序中.

import 一般是模块加入程序中.

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月17日 星期一 14:53

python st python.st在gmail.com
星期一 十二月 17 14:53:25 HKT 2007

°üÖУ¿   ʲôÒâ˼£¿  °ü£¿

ÔÚ07-12-17£¬jessinio liang <jessinio在gmail.com> дµÀ£º
>
> ÊéÉÏ˵,ʹÓÃfrom ... import ...Ò»°ãÊǼÓÈë°üÖеÄÄ£¿éµ½³ÌÐòÖÐ.
>
> import Ò»°ãÊÇÄ£¿é¼ÓÈë³ÌÐòÖÐ.
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
PythonÐÂÈË£¬ÕýÔÚ¶ñ²¹PythonÓï·¨£¬Èç¹ûÎÊÌâÖк¬Óа׳յÄÏÖÏ󣬾´ÇëÁ½⡣
Pythonѧϰ½ø¶È£º||||||||||||||||10%||||||||||||||||
Gtalk:python.st在gmail.com
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071217/3809d3c9/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月17日 星期一 15:10

头太晕 torrycn在gmail.com
星期一 十二月 17 15:10:16 HKT 2007

On Dec 17, 2007 2:53 PM, python st <python.st在gmail.com> wrote:

> °üÖУ¿   ʲôÒâ˼£¿  °ü£¿

°ü¾ÍÊÇÎļþ¼Ð(ÓÐ__init__.pyµÄ)
Ä£¿é¾ÍÊÇÎļþ(.py.pyc.pyoµÈ)
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071217/d7c27647/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月17日 星期一 15:50

python st python.st在gmail.com
星期一 十二月 17 15:50:04 HKT 2007

Ã÷°×ÁË£¬Ð»Ð»¡£

ÔÚ07-12-17£¬Í·Ì«ÔÎ <torrycn在gmail.com> дµÀ£º
>
>
>
> On Dec 17, 2007 2:53 PM, python st <python.st在gmail.com> wrote:
>
> > °üÖУ¿   ʲôÒâ˼£¿  °ü£¿
>
> °ü¾ÍÊÇÎļþ¼Ð(ÓÐ__init__.pyµÄ)
> Ä£¿é¾ÍÊÇÎļþ(.py.pyc.pyoµÈ)
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
PythonÐÂÈË£¬ÕýÔÚ¶ñ²¹PythonÓï·¨£¬Èç¹ûÎÊÌâÖк¬Óа׳յÄÏÖÏ󣬾´ÇëÁ½⡣
Pythonѧϰ½ø¶È£º||||||||||||||||10%||||||||||||||||
Gtalk:python.st在gmail.com
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071217/0a08be92/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月18日 星期二 12:55

Wayne moonbingbing在gmail.com
星期二 十二月 18 12:55:30 HKT 2007

一般无所谓,from a import b可以提高一点儿程序的效率。
不过如果程序大了,那么from a import * 带来的命名冲突绝对你带来的便利要多。
比如你在a文件里面from b import * 同时import datetime;b里面里面from c import *;而c里面from
datetime import datetime.
那么你在a文件里面使用datetime十有八九会有问题,这种错误还不是很好找
在07-12-17,python st <python.st at gmail.com> 写道:
>
>
> 明白了,谢谢。
>
> 在07-12-17,头太晕 <torrycn at gmail.com> 写道:
> >
> >
> >
> > On Dec 17, 2007 2:53 PM, python st <python.st at gmail.com> wrote:
> >
> > > 包中?   什么意思?  包?
> >
> > 包就是文件夹(有__init__.py的)
> > 模块就是文件(.py.pyc.pyo等)
> >
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> Python新人,正在恶补Python语法,如果问题中含有白痴的现象,敬请谅解。
> Python学习进度:||||||||||||||||10%||||||||||||||||
> Gtalk: python.st at gmail.com
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20071218/c8f69b6b/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号