6. Modules

2023-11-13

6. Modules

如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失。因此,如果你想要编写一些更大的程序,最好使用文本编辑器先编写好,然后运行这个文件。 这就是所谓的创建 脚本随着你的程序变得越来越长,你可能想要将它分成几个文件,这样更易于维护。你还可能想在几个程序中使用你已经编写好的函数,而不用把函数定义拷贝到每个程序中。

为了支持这个功能,Python 有种方法可以把你定义的内容放到一个文件中,然后在脚本或者交互方式中使用。这样的文件叫 模块; 一个模块中的的定义 可以被导入 到其他模块中或者导入到 模块 (the collection of variables that you have access to in a script executed at the top level and in calculator mode).

模块是包含 Python 定义和声明的文件。文件名就是模块名以 扩展名.py 结尾.在模块内部,模块名 (一个字符串) 可以通过一个全局变量 __name__取得.例如,用你最喜欢的文本编辑器在当前目录下创建一个名为fibo.py的文件,文件内容如下:

# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result

现在进入 Python 解释器并使用下面的命令导入这个模块:

>>>
>>> import fibo

在当前的符号表中,这并不导入 fibo 中定义的函数的名称,它只进入模块名称 fibo 。你可以通过模块名访问这些函数:

>>>
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'

如果你打算频繁使用一个函数,可以将它赋给一个本地的变量:

>>>
>>> fib = fibo.fib
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

6.1. More on Modules

模块可以包含可执行语句以及已定义的函数。这些语句通常用于初始化模块。它们只在 第一次 导入时执行。[1](如果文件以脚本的方式执行,它们也会运行。)

每个模块都有自己的私有符号表,这些私有符号表被该模块内定义的所有函数作为全局符号表使用。因此,模块的作者可以在模块里使用全局变量,而不用担心与某个用户的全局变量有冲突。另一方面,如果你非常清楚你在做什么,你就可以用同的符号来调用模块中的全局变量,modname。itemname 这种符号指向它的函数。

模块中可以导入其它模块。一般来说,习惯性地将所有 import 语句放在模块 (或脚本,就此而言) 的开头,但这不是必须的。被导入的模块的名字放在导入模块的全局符号表中。

另外有一种import 语句的变种,可以从一个模块中直接将名称导入模块的符号表中。例如:

>>>
>>> from fibo import fib, fib2
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

This does not introduce the module name from which the imports are taken in the local symbol table (so in the example, fibo is not defined).

还有种方式可以导入模块中定义的所有名字:

>>>
>>> from fibo import *
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

This imports all names except those beginning with an underscore (_). 大多数情况下Python程序员不要使用这个便利的方法,因为它会引入一系列未知的名称到解释器中,这很可能隐藏你已经定义的一些东西。

注意通常情况下从其他module或package中导入*是不被赞同的,因为这会降低代码的可读性。不过,在交互式会话中这样用是可以的,它可以让你少敲一些代码。

注意

出于性能考虑,每个模块在每个解释器会话中只导入一遍。因此, 如果你更改了你的模块,你必须重启解释器 – 或者, 如果你想用交互的方式测试你的模块, 可以使用importlib.reload(), e.g.import importlib; importlib.reload(modulename).

6.1.1. Executing modules as scripts

当你用下列的方式运行一个 Python 模块

python fibo.py <arguments>

the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__"这意味着,通过在你的模块末尾添加此代码︰

if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))

就可以让此文件既可以作为可执行的脚本,也可以当作可以导入的模块,因为解析命令行的那部分代码只有在模块作为 “main” 文件执行时才被调用:

$ python fibo.py 50
1 1 2 3 5 8 13 21 34

如果模块是被导入的,将不会运行这段代码:

>>>
>>> import fibo
>>>

这种方法通常用来为模块提供一个方便的用户接口,或者用来测试(例如直接运行脚本会执行一组测试用例)。

6.1.2. The Module Search Path

当一个叫spam 的模块被导入, 解释器会先在内置模块中搜索该模块.If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path.sys.path is initialized from these locations:

  • 脚本所在的目录(如果没有指明文件,则为当前目录)。
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • 与安装相关的默认值。

注意

 在支持符号连接的文件系统中,输入的脚本所在的目录是符号连接指向的目录。 换句话说也就是包含符号链接的目录会被加到目录搜索路径中。

After initialization, Python programs can modify sys.path脚本所在的目录被放置在搜索路径的最开始,也就是在标准库的路径之前。这意味着将会加载当前目录中的脚本,库目录中具有相同名称的模块不会被加载。除非你是有意想替换标准库,否则这应该被当成是一个错误。See section Standard Modules for more information.

6.1.3. “Compiled” Python files

To speed up loading modules, Python caches the compiled version of each module in the __pycache__ directory under the name module.version.pyc, where the version encodes the format of the compiled file; it generally contains the Python version number. For example, in CPython release 3.3 the compiled version of spam.py would be cached as __pycache__/spam.cpython-33.pyc这种命名约定允许由不同发布和不同版本的Python编译的模块同时存在。

Python checks the modification date of the source against the compiled version to see if it’s out of date and needs to be recompiled. 这是完全自动化的过程。同时,编译后的模块是跨平台的,所以同一个库可以在不同架构的系统之间共享。

Python 在两种情况下不检查缓存。First, it always recompiles and does not store the result for the module that’s loaded directly from the command line. 第二,如果没有源模块它不会检查缓存。若要支持没有源文件(只有编译版)的发布,编译后的模块必须在源目录下,并且必须没有源文件的模块。

部分高级技巧:

  • You can use the -O or -OO switches on the Python command to reduce the size of a compiled module. The -O switch removes assert statements, the -OO switch removes both assert statements and __doc__ strings. 因为某些程序可能会依赖于具有这些可用,您应只使用此选项,如果你知道你在做什么。“Optimized” modules have an opt-tag and are usually smaller. Future releases may change the effects of optimization.
  • A program doesn’t run any faster when it is read from a .pyc file than when it is read from a .py file; the only thing that’s faster about .pyc files is the speed with which they are loaded.
  • The module compileall can create .pyc files for all modules in a directory.
  • There is more detail on this process, including a flow chart of the decisions, in PEP 3147.

6.2. Standard Modules

Python 带有一个标准模块库,并发布有单独的文档叫Python 库参考手册(以下简称"库参考手册")。Some modules are built into the interpreter; these provide access to operations that are not part of the core of the language but are nevertheless built in, either for efficiency or to provide access to operating system primitives such as system calls. 这些模块是可配置的,也取决于底层的平台。For example, the winreg module is only provided on Windows systems. One particular module deserves some attention: sys, which is built into every Python interpreter. The variables sys.ps1 and sys.ps2 define the strings used as primary and secondary prompts:

>>>
>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> sys.ps1 = 'C> '
C> print('Yuck!')
Yuck!
C>

只有在交互式模式中,这两个变量才有定义。

变量sys.path是一个字符串列表,它决定了模块的解释器搜索路径。It is initialized to a default path taken from the environment variable PYTHONPATH, or from a built-in default ifPYTHONPATH is not set. 你可以使用标准的列表操作修改它:

>>>
>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')

6.3. The dir() Function

The built-in function dir() is used to find out which names a module defines. 它返回一个排好序的字符串列表:

>>>
>>> import fibo, sys
>>> dir(fibo)
['__name__', 'fib', 'fib2']
>>> dir(sys)  
['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__',
 '__package__', '__stderr__', '__stdin__', '__stdout__',
 '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe',
 '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv',
 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder',
 'call_tracing', 'callstats', 'copyright', 'displayhook',
 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
 'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
 'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount',
 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit',
 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
 'thread_info', 'version', 'version_info', 'warnoptions']

Without arguments, dir() lists the names you have defined currently:

>>>
>>> a = [1, 2, 3, 4, 5]
>>> import fibo
>>> fib = fibo.fib
>>> dir()
['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']

注意它列出了所有类型的名称: 变量、 模块、 函数等。

dir() does not list the names of built-in functions and variables. If you want a list of those, they are defined in the standard module builtins:

>>>
>>> import builtins
>>> dir(builtins)  
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',
 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
 'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError',
 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
 'NotImplementedError', 'OSError', 'OverflowError',
 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError',
 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning',
 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError',
 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',
 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__',
 '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs',
 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable',
 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',
 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',
 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',
 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',
 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview',
 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars',
 'zip']

6.4. Packages

包是一种管理 Python 模块命名空间的方式,采用“点分模块名称”。例如,模块名称A.B指定了包A 中名为B 的子模块。就像模块的使用让不同模块的作者不用担心相互间的全局变量名称一样,点分模块的使用让包含多个模块的包(例如 Numpy 和 Python Imaging Library)的作者也不用担心相互之间的模块重名。

假设你想要设计一系列模块(或一个“包”)来统一处理声音文件和声音数据。有很多不同的声音文件格式 (通常承认其扩展名,例如︰ .wav.aiff.au),所以您可能需要创建和维护日益模块集合的各种文件格式之间的转换。你可能还想针对音频数据做很多不同的操作(比如混音,添加回声,增加均衡器功能,创建人造立体声效果),所以你还需要编写一组永远写不完的模块来处理这些操作。你的包可能会是这个结构(用分层的文件系统表示):

sound/                          Top-level package
      __init__.py               Initialize the sound package
      formats/                  Subpackage for file format conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py
              ...
      effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py
              ...
      filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py
              ...

当导入包,Python 通过 sys.path 寻找包子目录的目录搜索。

__init__.py 文件是必需,使 Python 当作目录包含包;这样做的目的是为了防止无意中隐藏的模块搜索路径之后可能发生的有效模块具有一个共同的名字,如 string,目录。最简单的情况,__init__.py 可以只是一个空的文件,但它也可以为包执行初始化代码或设置 __all__ 变量,稍后介绍。

用户可以从包中导入单独的模块,例如:

import sound.effects.echo

这将加载子模块 sound.effects.echo它必须使用其完整名称来引用。

sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)

导入子模块的另一方法是:

from sound.effects import echo

这样也能加载子模块 echo, 这样就不需要写前缀了;因此它也能像下面这样使用:

echo.echofilter(input, output, delay=0.7, atten=4)

还有另一种变化方式是直接导入所需的函数或变量:

from sound.effects.echo import echofilter

同样, 也就可以这样导入子模块 echo, 它使得它的函数 echofilter()直接就可用了:

echofilter(input, output, delay=0.7, atten=4)

Note that when using from package import item, the item can be either a submodule (or subpackage) of the package, or some other name defined in the package, like a function, class or variable. The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it. If it fails to find it, an ImportError exception is raised.

Contrarily, when using syntax like import item.subitem.subsubitem, each item except for the last must be a package; the last item can be a module or a package but can’t be a class or function or variable defined in the previous item.

6.4.1. Importing * From a Package

当用户输入 from sound.effects import *时会发生什么?理想情况下,他应该是希望到文件系统中寻找包里面有哪些子模块,并把它们全部导入进来。这可能需要很长时间,而且导入子模块可能会产生想不到的副作用,这些作用本应该只有当子模块是显式导入时才会发生。

唯一的解决办法是包的作者为包提供显式的索引。The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. 当包有新版本包发布时,就需要包的作者更新这个列表了。如果包的作者认为不可以用 import * 方式导入它们的包,也可以决定不支持它。For example, the file sound/effects/__init__.py could contain the following code:

__all__ = ["echo", "surround", "reverse"]

This would mean that from sound.effects import * would import the three named submodules of the sound package.

If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.pyIt also includes any submodules of the package that were explicitly loaded by previous import statements. 请考虑此代码:

import sound.effects.echo
import sound.effects.surround
from sound.effects import *

In this example, the echo and surround modules are imported in the current namespace because they are defined in the sound.effects package when the from...importstatement is executed. (This also works when __all__ is defined.)

Although certain modules are designed to export only names that follow certain patterns when you use import *, it is still considered bad practise in production code.

Remember, there is nothing wrong with using from Package import specific_submodule事实上,这是推荐的写法,除非导入的模块需要使用其它包中的同名子模块。

6.4.2. Intra-package References

When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module sound.filters.vocoder needs to use the echo module in the sound.effects package, it can use from sound.effects import echo.

You can also write relative imports, with the from module import name form of import statement. 这些导入使用前导的点号表示相对导入的是从当前包还是上级的包。From thesurround module for example, you might use:

from . import echo
from .. import formats
from ..filters import equalizer

注意,相对导入基于当前模块的名称。Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.

6.4.3. Packages in Multiple Directories

Packages support one more special attribute, __path__This is initialized to be a list containing the name of the directory holding the package’s __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.

虽然通常不需要此功能,它可以用于扩展包中的模块的集合。

脚注

[1] In fact function definitions are also ‘statements’ that are ‘executed’; the execution of a module-level function definition enters the function name in the module’s global symbol table.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

6. Modules 的相关文章

  • VMWare Workstation 16 安装 Ubuntu 22.04 LTS

    最近想编译Android8 1 系统源码 不太想安装双系统 先尝试用虚拟机安装Ubuntu来编译试试 过程中也遇到一些特殊的错误 因此做了一次记录 VMWare Workstation 16 的下载和安装这里不作介绍 网上也有很多注册码可用
  • (Animator详解一)mixamo动画导入Unity的一些配置

    Mixamo是Adobe公司出品的免费动画库 可商用 软件分为characters 角色 Animations 动画 两个部分 下方的搜索框可以搜寻你想要的动作动画 网址为 Mixamo 搜索框的子菜单表示动画的类别 当我们的项目需要角色动
  • 【Xilinx Vivado时序分析/约束系列2】FPGA开发时序分析/约束-建立时间

    目录 基本概念 数据结束时间 Data finish time 保持时间门限 保持时间余量 Hold Slack 基本概念 数据结束时间 Data finish time 之前解释了数据达到的时间 对于data arrival time T
  • cpu矿工cpuminer-multi编译与使用

    文章目录 编译步骤 cpuminer multi 矿工运行 cpuminer multi有很多不同前辈开发 这里选用star最多且最流行的 lucasjones cpuminer multi 在编译中遇到了很多坑 这里全部整合到流程中 如果
  • nimg 文件服务器,NIMG-45. DEEP LEARNING-BASED PERITUMORAL MICROSTRUCTURE MAPPING IN GLIOBLASTOMAS USING FR...

    摘要 PURPOSE Characterization of the peritumoral microenvironment is a widely researched but as yet unsolved problem Deter
  • K-近邻算法预测电影类型

    K 近邻算法预测电影类型 k 近邻算法是一种比较简单 但是在一些方面又有很多作用的算法 比较常用的就是推荐入住位置 或者推荐入住酒店等等 K 近邻算法的原理 就是根据特征值 计算出离自己最近的那个分类 自己也属于那个类别 K 近邻是一种分类
  • 吴恩达机器学习(三) 无监督学习

    Unsupervised Learning Unsupervised learning allows us to approach problems with little or no idea what our results shoul
  • Django框架

    目录 目录 一 虚拟环境 1 什么是虚拟环境 2 作用 3 wondows下安装使用 二 Django框架 1 安装Django 2 拓展 虚拟机和虚拟环境问题 2 1虚拟机的三种网络模式 3 创建Django项目 3 1完整创建Djang
  • Python中Print()函数的用法___实例详解(全,例多)

    Python中Print 函数的用法 实例详解 全 例多 目 录 一 print 函数的语法 二 print 打印输出文本 三 print 中空格的使用方法 四 Print 换行 五 区隔符 sep 六 制表符 t 七 输出数学表达式 八
  • Qt:可视化UI设计

    1 创建项目 修改组件的对象名字和显示文本内容 创建一个 Widget Application 项目类 QDialog 在创建窗体时选择基类 QDialog 生成的类命名为 QWDialog 并选择生成窗体 在界面设计时 对需要访问的组件修
  • AES 配合mybaties 实现指定字段自动加解密

    1 加密工具类 Slf4j public class AESUtil 密钥长度 128 192 or 256 private static final int KEY SIZE 256 加密 解密算法名称 private static fi

随机推荐

  • C/C++从字符串中提取出数字的方法回顾

    在对格式化的数据进行处理的时候 很多时候需要在字符串中进行数据的提取 如果使用Oracle数据库 可以用里面的非常强大的sqlldr功能进行数据的提取和导入 在C C 中 可以自定义个方法提取出数字字符串 再使用atoi atof之类的方法
  • 颜色空间之RGB与YUV

    此篇是我在学习中做的归纳与总结 其中如果存在版权或知识错误或问题请直接联系我 欢迎留言 PS 本着知识共享的原则 此篇博客可以转载 但请标明出处 RGB CIE1931 RGB系统选择了700nm R 546 1nm G 435 8nm B
  • VGGNet实现CIFAR-100图像识别-1(数据预处理,one-hot)

    VGGNet CIFAR 100 导入数据 数据预处理 方法1 方法2 可能会遇到的问题 解决办法 Normalization和拆分训练集 验证集 One hot编码 未完待续 接下来请看另一篇博文 VGGNet实现CIFAR 100图像识
  • js复制功能插件

    JavaScript内容复制插件Clipboard js
  • 《C语言编程魔法书:基于C11标准》——1.3 主流C语言编译器介绍

    本节书摘来自华章计算机 C语言编程魔法书 基于C11标准 一书中的第1章 第1 3节 作者 陈轶 更多章节内容可以访问云栖社区 华章计算机 公众号查看 1 3 主流C语言编译器介绍 对于当前主流桌面操作系统而言 可使用Visual C GC
  • ARMV8体系结构简介:AArch64系统级体系结构之存储模型

    1 前言 关于存储系统体系架构 可以概述如下 存储系统体系结构的形式 VMSA 存储属性 2 存储系统体系结构 2 1 地址空间 指令地址空间溢出 指令地址计算 address of current instruction size of
  • Xcode14 终于放弃了bitcode和armv7架构,还有iOS 9、iOS 10

    相信大家已经了解到了不少关于Xcode 14的新消息 什么精简安装包 按需下载功能模块 提升编译速度 更快的xib storyBoard和SwiftUI app icon 1024像素图片 Xcode 14还放弃了一些东西 1 放弃了bit
  • openssl md5

    关于 16位和32位 md5得到的是一个16字节的散列值 每个字节用16进制 0x 格式成两个字符 连起来得到一个32个字符的串这就是所说的32位 16位就是取的32位的中间段 md5 aabbccdd 32位 bf3b2290e229da
  • (海伦公式)已知三角形三条边长,求面积

    海伦公式 已知三角形三条边长 求面积 海伦公式 S p p a p b p c 其中p是三角形的周长的一半p a b c 2 以下转自百度百科 海伦公式海又译作希伦公式 海龙公式 希罗公式 海伦 秦九韶公式 传说是古代的叙拉古国王 希伦 H
  • jQuery与原生JS相互转化

    前端发展很快 现代浏览器原生 API 已经足够好用 我们并不需要为了操作 DOM Event 等再学习一下 jQuery 的 API 同时由于 React Angular Vue 等框架的流行 直接操作 DOM 不再是好的模式 jQuery
  • WSL 2(Ubuntu18.04)编译Linux内核(5.7.9)并替换掉WSL 2原有内核

    准备工作 配置库 由于编译过程中需要很多库 因此需要提前进行配置 如果编译过程中遇到的报错均在下文的报错信息中记录 准备安装的库的命令为 sudo apt get install libncurses5 dev libncursesw5 d
  • Android NDK Address Sanitizer

    文章目录 构建 运行 堆栈轨迹 二进制测试 此文章是基于官方文档 Address Sanitizer的基础上做了一些扩展说明 从 API 级别 27 Android O MR 1 开始 Android NDK 可支持 Address San
  • 华为OD机试真题2022Q4 A + 2023 B卷(Java)

    大家好 我是哪吒 五月份之前 如果你参加华为OD机试 收到的应该是2022Q4或2023Q1 这两个都是A卷题 5月10日之后 很多小伙伴收到的是B卷 那么恭喜你看到本文了 抓紧刷题吧 B卷新题库正在更新中 华为机试有三道题 第一道和第二道
  • 安装win10 和ubuntu18.04双系统时 device for boot installation的选择

    最近在笔记本上安装ubuntu18 04 电脑预装了win10 有两个盘 128G的SSD 1T的HDD win10装在了SSD上 磁盘是GPT UEFI启动模式 在HDD上压缩了空间安装Ubuntu 在安装界面上有一个 Device fo
  • Selenium - Tracy 小笔记2

    selenium本身是一个自动化测试工具 它可以让python代码调用浏览器 并获取到浏览器中加们可以利用selenium提供的各项功能 帮助我们完成数据的抓取 它容易被网站识别到 所以有些网站爬不到 它没有逻辑 只有相应的函数 直接搜索即
  • PAT乙级1052 卖个萌 (20 分)测试点123

    https pintia cn problem sets 994805260223102976 problems 994805273883951104 测试点0 Are you kidding me 中 为转义字符 要用双 表示 测试点1
  • 动态规划经典例题-最长公共子序列-python

    最长公共子序列 问题描述 题解 以问题中为例 A helloworld B loop res i j 表示 截止到B的第i个字符和截止到A的第j个字符的最长公共子序列 例如 res 2 5 2表示第2行第5列 也就是lo和hello的最长公
  • centos7 安装 bugfree3

    1 安装apache yum install httpd 2 安装mysql wget i c http dev mysql com get mysql57 community release el7 10 noarch rpm yum y
  • set的特点

    set不允许元素重复且无序 常用实现有HashSet LinkedHashSet和TreeSet HashSet通过HashMap实现 HashMap的key即HashSet存储的元素 所有key都使用相同的Value 一个名为PRESNT
  • 6. Modules

    6 Modules 如果你退出 Python 解释器并重新进入 你做的任何定义 变量和方法 都会丢失 因此 如果你想要编写一些更大的程序 最好使用文本编辑器先编写好 然后运行这个文件 这就是所谓的创建 脚本 随着你的程序变得越来越长 你可能