zoukankan      html  css  js  c++  java
  • python import语句

    从模块导入函数的时候,可以使用

    import somemodule

    或者

    from somemodule import somefunction
    from somemodule import somefunction, anotherfunction, yetanotherfunction

    或者

    from somemodule import *

    只有确定自己想要从给定得模块导入所有功能时,才应该使用最后一个版本。但是如果两个模块都有open函数,那又该怎么办?只需使用第一种方式导入,然后像下面这样使用函数:

    module1.open(...)
    module2.open(...)

    但还有另外的选择:可以在语句末尾增加一个as子句,在该子句后给出名字,或为整个模块提供别名:

    In [23]: import math as foobar

    In [24]: foobar.sqrt(4)
    Out[24]: 2.0

    也可以为函数提供别名:

    In [28]: from math import sqrt as foobar

    In [29]: foobar(4)
    Out[29]: 2.0

    对于open函数,可以像下面这样使用:

    from module1 import open as open1
    from module2 import open as open2









  • 相关阅读:
    无锁编程(五)
    Linux Kernel CMPXCHG函数分析
    无锁编程(四)
    无锁编程(三)
    无锁编程(二)
    无锁编程(一)
    无锁编程
    Linux同步机制
    Linux同步机制
    bootstrap css编码规范
  • 原文地址:https://www.cnblogs.com/skyhacker/p/2328806.html
Copyright © 2011-2022 走看看