• import学习


    一、import  as
        import socket, os, regex模块导入时可以使用 as 关键字来改变模块的引用对象名字:

        import os as system

        //当多个引入时
        import socket as net, thread as threads
        system.chdir("..")
        net.gethostname()

    二、from  import  

        使用from语句可以将模块中的对象直接导入到当前的名字空间. from语句不创建一个到模块名字空间的引用对象,而是把被导入模块的一个或多个对象直接放入当前的名字空间:

        from socket import gethostname
                                   # 将gethostname放如当前名字空间
        print gethostname()            # 直接调用
        socket.gethostname()           引发异常NameError: socket

        //from语句支持逗号分割的对象,也可以使用星号(*)代表模块中除下划线开头的所有对象: 

        from socket import gethostname, socket
        from socket import *   # 载入所有对象到当前名字空间

    三、另外, as 也可以和 from 联合使用:

        from socket import gethostname as hostname
        h = hostname()

  • 相关阅读:
    ubuntu分辨率
    xubuntu无法进图形界面问题
    dl简单模板,无pretraining过程
    ubuntu远程失败xrdp重启命令
    强制ubuntu登陆用户退出
    NumPy for MATLAB users
    How to calculate bits per character of a string? (bpc) to read
    ubuntu ssh前后台切换命令相关
    samba共享文件夹设置
    Ubuntu下环境变量的设置
  • 原文地址:https://www.cnblogs.com/gczr/p/6668687.html
Copyright © 2020-2023  润新知