python in module_自定义内建模块 - Python Build Your Own Built-In Module

1 在 python 中, 用户可以通过 py 文件创建自定义的 module, 也可以通过 C 创建 dll, 扩展 python module.2 当用户在一个正在编辑的模块 module 中, 引入(import)另一个已经编辑好的 module 的时候,3 需要名字指明另一个 module 的所在位置,python 才能成功 import该模块.4 例如,5 在 A.py 中 import abc 文件夹下的 123module,6 A.py,7 import abc/123

8

9 目录结构如下,10 A.py11 -abc12 - 123.py13

14 然而当引入 built in module 的时候, 只需要以 import + name of built inmodule 的形式即可,15 如 import sys . 这是为什么呢?为什么 python 会知道所要 import 的 built inmodule 所在的位置呢?16 毫无疑问, 一定是有这么一个机制 - 能否告诉 python 所引用的 built inmodule 在什么地方儿呢,17 所以对与 built in module 的 import 来说,在 import的时候不需要显示的指出 module 所在位置.18

19 下面来具体看一下儿,20 在 python 启动之后, python 已经创建了一个 名字空间 namespace,21 在这个 namespace 中的符号和值来至于系统 module.而这些系统 module 中,22 __builtin__module 就是其中一个被创建的 module .23 而 python 会创建一个 PyDictObject 对象,来维护系统所有 modules.24

25 static PyMethodDef builtin_methods[] ={26 {"__built_class__", (PyCFunction)builtin___built_class__,METH_VARARGS |METH_KEYWORDS, built_class_doc},27 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS |METH_KEYWORDS, import_doc},28 {"dir", builtin_dir, METH_VARARGS |METH_KEYWORDS, built_class_doc}, METH_VARARGS, dir_doc},29 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},30 {"iter", builtin_iter, METH_VARARGS, iter_doc},31 {"max", (PyCFunction)builtin_max, METH_VARARGS |METH_KEYWORDS, max_doc},32 {"min", (PyCFunction)builtin_min, METH_VARARGS |METH_KEYWORDS, min_doc},33 {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc},34 {"print", (PyCFunction)builtin_print, METH_VARARGS |METH_KEYWORDS, print_doc},35 {"round", (PyCFunction)builtin_round, METH_VARARGS |METH_KEYWORDS, round_doc},36 {"vars", builtin_vars, METH_VARARGS, vars_doc},};37

38 设置完 builtin methods 的属性, 接下就需要这个一个函数, 可以告诉 python 所引入 builtin module 的搜索路径了.39 这个函数就是,40 void Py_SetPath(const wchar_t *)41

42 python docs org 上面的解释如下,43 Set the default module search path. If this function iscalled before44 Py_Initialize(),then Py_GetPath() won’t attempt to compute a default45 search path but uses the one provided instead.This is useful ifPython46 isembedded by an application that has full knowledge of the location47 of all modules.The path components should be separated by the platform48 dependent delimiter character,which is ':' on Unix andMac OS X,49 ';'on Windows.50 This also causes sys.executable to be set only to the raw program name51 (see Py_SetProgramName()) and for sys.prefix andsys.exec_prefix to be52 empty. It is up to the caller to modify these ifrequired after calling53 Py_Initialize().Use Py_DecodeLocale() to decode a bytes string to get54 a wchar_* string.The path argument iscopied internally, so the caller55 may free it after the call completes.56

57 最后,来一起看一个添加自定义 builtin module 的示例,58 例子,59 其实很简单, 值需要将自定义模块(zzyzz.py), 放在路径 "\Python\Python36-32\lib"下就可以了,60 zzyzz.py61 + importdatetime62 defweeknumber():63 print ("Week - %s" % datetime.date.today().isocalendar()[1])64 print ("Date - %s" %str(datetime.date.today().isocalendar()))65 print ("Date - %s" %str(datetime.date.today()))66

67 Output,68 >>>importzzyzz69 >>>zzyzz.weeknumber()70 Week - 43

71 Date - (2017, 43, 1)72 Date - 2017-10-23

73 >>>sys.modules['zzyzz']74

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值