7、Python使用外部库

导入、运算符重载以及冒险进入外部库世界的生存技巧!

在本教程中,您将了解 Python 中的Imports,获得一些使用不熟悉的库(以及它们返回的对象)的技巧,并深入研究运算符重载

文章目录

  • 1.导入(Imports)
    • 其他导入语法
    • 子模块
    • 哦,你将去的地方,哦,你将看到的对象
    • 了解奇怪对象的三种工具
  • 2.运算符重载
    • 什么时候 1 + 1 不等于 2?
    • 好奇这一切是怎么运作的吗?

1.导入(Imports)

到目前为止,我们已经讨论了python语言中内置的类型和函数。

但 是关于 Python 最好的一点之一(特别是对于数据科学家而言)是为其编写的大量高质量的自定义库。

其中一些库位于“标准库”,这意味着你可以在运行 Python 的任何地方找到它们。其他库可以很容易地添加,即使它们并不总是与 Python 一起提供。

无论哪种方式,我们都将使用导入Imports来访问这些代码。

我们将从标准库中导入 math 开始我们的示例。

In [1]:

import math

print("It's math! It has type {}".format(type(math)))
It's math! It has type module'>

math是个模块。模块只是由其他人定义的变量(如果愿意,可以称之为命名空间)的集合。我们可以使用内置函数 dir ()查看math中的所有名称。

In [2]:

print(dir(math))
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

我们可以使用点语法来访问这些变量,其中一些引用了简单的值,比如 math.pi:

In [3]:

print("pi to 4 significant digits = {:.4}".format(math.pi))
pi to 4 significant digits = 3.142

但是我们在这个模块中发现的大部分都是函数,比如 math.log:

In [4]:

math.log(32, 2)

Out[4]:

5.0

当然,如果我们不知道 math.log 是做什么的,我们可以在它上面调用 help () :

In [5]:

help(math.log)
Help on built-in function log in module math:

log(...)
    log(x, [base=math.e])
    Return the logarithm of x to the given base.
    
    If the base not specified, returns the natural logarithm (base e) of x.

我们还可以对模块本身调用 help()。这将为我们提供模块中所有函数和值的组合文档(以及模块的高级描述)。点击 “output” 按钮查看完整的 math 帮助页面。

In [6]:

help(math)

output

Help on module math:

NAME
    math

MODULE REFERENCE
    https://docs.python.org/3.7/library/math
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    This module provides access to the mathematical functions
    defined by the C standard.

FUNCTIONS
    acos(x, /)
        Return the arc cosine (measured in radians) of x.
    
    acosh(x, /)
        Return the inverse hyperbolic cosine of x.
    
    asin(x, /)
        Return the arc sine (measured in radians) of x.
    
    asinh(x, /)
        Return the inverse hyperbolic sine of x.
    
    atan(x, /)
        Return the arc tangent (measured in radians) of x.
    
    atan2(y, x, /)
        Return the arc tangent (measured in radians) of y/x.
        
        Unlike atan(y/x), the signs of both x and y are considered.
    
    atanh(x, /)
        Return the inverse hyperbolic tangent of x.
    
    ceil(x, /)
        Return the ceiling of x as an Integral.
        
        This is the smallest integer >= x.
    
    copysign(x, y, /)
        Return a float with the magnitude (absolute value) of x but the sign of y.
        
        On platforms that support signed zeros, copysign(1.0, -0.0)
        returns -1.0.
    
    cos(x, /)
        Return the cosine of x (measured in radians).
    
    cosh(x, /)
        Return the hyperbolic cosine of x.
    
    degrees(x, /)
        Convert angle x from radians to degrees.
    
    erf(x, /)
        Error function at x.
    
    erfc(x, /)
        Complementary error function at x.
    
    exp(x, /)
        Return e raised to the power of x.
    
    expm1(x, /)
        Return exp(x)-1.
        
        This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    
    fabs(x, /)
        Return the absolute value of the flo

你可能感兴趣的:(从零开始的Python之旅,python,开发语言)