V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tanteng
V2EX  ›  Python

关于 Python 里 import *的问题

  •  
  •   tanteng ·
    tanteng · 2015-05-12 15:28:04 +08:00 · 3571 次点击
    这是一个创建于 3280 天前的主题,其中的信息可能已经有所发展或是发生改变。

    from adsl import *
    比如这个,为什么要这样写?直接import adsl不就行了

    18 条回复    2015-05-13 00:10:53 +08:00
    ladrift
        1
    ladrift  
       2015-05-12 15:38:38 +08:00 via Android   ❤️ 2
    import adsl
    模块中的名字全部是adsl这个名字下的
    比如调用adsl中的foo函数(举个栗子):
    adsl.foo()
    而from adsl import *
    可以用
    foo()
    直接调用foo函数
    tanteng
        2
    tanteng  
    OP
       2015-05-12 15:45:34 +08:00
    monsabre1
        3
    monsabre1  
       2015-05-12 15:48:29 +08:00
    from adsl import *


    这种不好
    ladrift
        4
    ladrift  
       2015-05-12 15:49:19 +08:00 via Android   ❤️ 1
    @tanteng 推荐一个Python学习资源
    http://www.fullstackpython.com/
    共勉
    tanteng
        5
    tanteng  
    OP
       2015-05-12 15:57:38 +08:00
    @monsabre1 理由?
    mulog
        6
    mulog  
       2015-05-12 16:23:08 +08:00
    @tanteng 污染 namespace, 而且你自己不知道 import 了些什么乱七八糟的进来
    washinriver
        7
    washinriver  
       2015-05-12 17:02:07 +08:00
    一般写成 from adsl import func1,func2
    方便,清楚.
    binux
        8
    binux  
       2015-05-12 17:03:15 +08:00   ❤️ 2
    我知道我为什么写不出博客了。。
    twor2
        9
    twor2  
       2015-05-12 17:30:27 +08:00
    @binux 大神,对自己的要求太高
    MrEggNoodle
        10
    MrEggNoodle  
       2015-05-12 17:42:05 +08:00
    @binux 哈哈哈。总觉得你这句话很幽默。
    no13bus
        11
    no13bus  
       2015-05-12 20:55:46 +08:00
    @binux 哈哈。其实我觉得他是没有把这个页面右上方那些基础书籍看一遍,有些基础的知识在书里面是找的到的。
    lcqtdwj
        12
    lcqtdwj  
       2015-05-12 21:50:21 +08:00
    产品一般不会 from XXX import *,因为会污染命名空间,locals()看一下,如果有相同名字可能就冲突了。一般在ipyhton里图省事这样写。
    laike9m
        13
    laike9m  
       2015-05-12 22:01:04 +08:00
    @binux 可以写非技术的啊( ゚∀゚)o彡゚
    fatpa
        14
    fatpa  
       2015-05-12 22:36:38 +08:00
    可怕!
    mingyun
        15
    mingyun  
       2015-05-12 22:37:46 +08:00
    污染命名空间
    beibeijia
        16
    beibeijia  
       2015-05-12 22:47:18 +08:00
    一般这么用也就是图个方便 特别是学习和测试的时候 比如pyqt from PyQt4.QtGui import * 这样调用的时候 直接QWidget() QPushButton() 否则 PyQt4.QtGui.QWidget() PyQt4.QtGui.QPushbutton() 每次这么写太麻烦了 不过正式写程序搞项目的时候这种写法还是应该少用 就像楼上说的会污染命名空间 像这样导入三个以上的模块 如果其中有命名发生重叠 你调用的方法或者变量就有可能不是你想要的 最终有可能导致程序崩溃或者不按照你想要的方式执行 关于这方面卤煮可以去看看《python学习手册》这里对核心知识的一些细节讲的还是比较到位的。
    tanteng
        17
    tanteng  
    OP
       2015-05-12 23:43:50 +08:00
    @fatpa import *这样用不好是吧
    enotx
        18
    enotx  
       2015-05-13 00:10:53 +08:00 via Android
    @tanteng 是的,简单来说就是不要用from xxx import *,宁愿import xxx
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2368 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 10:26 · PVG 18:26 · LAX 03:26 · JFK 06:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.