设为首页 收藏本站
查看: 327|回复: 0

[经验分享] Beginning Python, 3rd Edition(Chapter 10)

[复制链接]
累计签到:161 天
连续签到:1 天
发表于 2017-4-19 12:01:58 | 显示全部楼层 |阅读模式
Batteries Included(CHAPTER 10)

1. tell your interpreter where to look for the module by executing the following.
        >>> import sys
        >>> sys.path.append('/home/zhanglei')  # In UNIX, you cannot simply append the string '~/python' to sys.path. You must use the full path (such as '/home/yourusername/python')
        >>> sys.path #显示interpreter搜索的路径
2.you try to import it again,nothing happens.
        >>> import hello
        >>>
        They are mostly meant to define things, such as variables, functions, classes, and so on.
        And because you need to define things only once, importing a module several times has the same effect as
        importing it once.
3.hello2.py
        **************************
         #!/usr/bin/env python
         def hello2():
     print 'hello2'
        **************************
        >>> import hello2
        >>> hello2.hello()
4.hello3.py
        **************************
        #!/usr/bin/env python
        def hello():
    print 'hello,world!'
        # A test:
        hello()
**************************
5. pprint is a pretty-printing function, which makes a more intelligent printout.
        >>> import sys, pprint
        >>> pprint.pprint(sys.path)
        As long as your module is located in a place like site-packages('/usr/lib/python2.7/site-packages'), all your programs will be able to import it.
6.PYTHONPATH:
        The standard method is to include your module directory (or directories) in the environment variable PYTHONPATH(Environment variables are not part of the Python interpreter)
        export PYTHONPATH=/home/zhanglei/python
        Tab补全:pythonTab.py(/home/zhanglei/python)
        export PYTHONPATH=$PATHPATH:~/python
        import sys
        import readline
        import rlcompleter
        import atexit
        import os
        # tab completion
        readline.parse_and_bind('tab: complete')
        # history file
        histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
7.To make Python treat it as a package,it must contain a file named __init__.py.
        You can also nest packages inside other packages.
                File/Directory                                        Description
                ~/python/                                                Directory in PYTHONPATH
                ~/python/drawing/                                Package directory (drawing package)
                ~/python/drawing/__init__.py        Package code (drawing module)
                ~/python/drawing/colors.py                colors module
                ~/python/drawing/shapes.py                shapes module
       
        import drawing                                                # (1) Imports the drawing package
        import drawing.colors                                # (2) Imports the colors module
        from drawing import shapes                        # (3) Imports the shapes module
8.To find out what a module contains, you can use the dir function:
        dir(copy)
        [n for n in dir(copy) if not n.startswith('_')]
        (Several of these names begin with an underscore—a hint (by convention) that they aren’t meant to be used outside the module.)
9.        from copy import *
        you get only the four functions listed in the __all__ variable.
        If you don’t set __all__, the names exported in a starred import defaults to all global names in the module that don’t begin with an underscore
10.        help(copy.copy):
        A docstring is simply a string you write at the beginning of a function to document it
        >>> print(copy.copy.__doc__)


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-366387-1-1.html 上篇帖子: 关于老男孩python第14期python 下篇帖子: Python 安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表