zoukankan      html  css  js  c++  java
  • 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()

  • 相关阅读:
    Codeforces 868A Bark to Unlock
    poj 3744 Scout (Another) YYF I
    Codeforces 526F Pudding Monsters
    Codeforces 789D Weird journey
    Codeforces 827E Rusty String
    Codeforces 841D Leha and another game about graph
    bzoj 3560 DZY Loves Math V
    Codeforces 833A The Meaningless Game
    Codeforces 839D Winter is here
    Codeforces 839C Journey
  • 原文地址:https://www.cnblogs.com/gczr/p/6668687.html
Copyright © 2011-2022 走看看