Easton's Blog Easton's Blog
首页
  • 编程语言

    • Python
  • 框架

    • Django
  • Mdn (opens new window)
  • HTML
  • CSS
  • JavaScript
  • Mysql
  • PostgreSQL
  • Elasticsearch
  • MongoDB
  • Redis
  • 服务器命令
  • Docker
  • GIT
  • 摄影
  • 草稿
  • 归类方式

    • 分类
    • 标签
    • 归档
  • 关于博客

    • 博客教程
    • 友情链接
    • 关于
导航站
GitHub (opens new window)

Easton Yang

爱生活😊爱学习
首页
  • 编程语言

    • Python
  • 框架

    • Django
  • Mdn (opens new window)
  • HTML
  • CSS
  • JavaScript
  • Mysql
  • PostgreSQL
  • Elasticsearch
  • MongoDB
  • Redis
  • 服务器命令
  • Docker
  • GIT
  • 摄影
  • 草稿
  • 归类方式

    • 分类
    • 标签
    • 归档
  • 关于博客

    • 博客教程
    • 友情链接
    • 关于
导航站
GitHub (opens new window)
  • Python

    • Python学习

      • 特殊方法
      • 常用内置函数
      • 数据类型
      • with用法
      • Python面向对象
      • re正则
      • 线程
      • 进程
      • 协程
      • 装饰器
      • 堆队列heapq
      • 容器collections
      • 内置异常
      • 枚举
      • 好玩的函数
        • dir
        • dis
        • help
        • inspect 和 importlib
    • 代码

    • 数据结构与算法

  • Django

  • 后端
  • Python
  • Python学习
eastonyangxu
2023-09-04
目录

好玩的函数

# dir

dir python3.8 官方文档 (opens new window)

import sys
# dir:如果没有实参,则返回当前本地作用域中的名称列表。如果有实参,它会尝试返回该对象的有效属性列表。
print(dir(sys))
print(sys.stdout.__doc__)  # 查看文档

# 包含了所有的被编译进 Python 解释器的模块(这个信息无法通过其他的办法获取, modules.keys() 只包括被导入过的模块。)
print(sys.builtin_module_names)
1
2
3
4
5
6
7

# dis

dis python3.8 官方文档 (opens new window)

import dis

# dis 字节码反汇编器
def myfunc(alist):
    return len(alist)

dis.dis(myfunc)
1
2
3
4
5
6
7

# help

# help:不带参数时,显示可用的命令列表。参数为 command 时,打印有关该命令的帮助
help(str.encode)
1
2

# inspect 和 importlib

inspect python3.8 官方文档 (opens new window) importlib python3.8 官方文档 (opens new window)

import importlib
import inspect

def demo(a, b=None, c: 'int>0' = 20):
    if not b:
        check = 0
    else:
        check = b
    if isinstance(a, int):
        return a + check + c
    else:
        return None


# inspect 模块的使用  https://docs.python.org/zh-cn/3.8/library/inspect.html#module-inspect
# 该模块提供了4种主要的功能:类型检查、获取源代码、检查类与函数、检查解释器的调用堆栈。
sig = inspect.signature(demo)  # 获取函数的参数信息
print(sig.return_annotation)
for param in sig.parameters.values():
    note = repr(param.annotation).ljust(13)
    print(note, ':', param.name, '=', param.default)

# 动态加载模块  importlib模块的使用  https://docs.python.org/zh-cn/3.8/library/importlib.html#module-importlib
module = importlib.import_module('interview.base_study')  # 绝对引入
# module = importlib.import_module('.base_study', package='interview')  # 相对引入,注意前面的点
print([func for name, func in inspect.getmembers(module, inspect.isfunction)])  # 获取模块中的所有方法
print(dir(module))  # 查看模块中的所有属性
for i in dir(module):
    if not i.startswith('_') or not i.endswith('_'):
        print(getattr(module, i))  # 动态获取模块中的属性


# =================================
# globals() 返回一个字典, 表示当前的全局符号表。 这个符号表始终针对当前
# 模块(对函数或方法来说, 是指定义它们的模块, 而不是调用它们的模
# 块) 。  6.1.2中有使用到

import inspect
import zip_study

# 查找 zip_study 模块中的所有函数 ,可以实现动态获取属性。 6.1.2中有使用到
print(inspect.getmembers(zip_study, inspect.isfunction))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#Python
上次更新: 2023/09/04, 18:39:45
枚举
aes案例

← 枚举 aes案例→

最近更新
01
攻略制作要点
07-18
02
摄影主题拍摄
07-18
03
延时摄影剧本
07-18
更多文章>
Theme by Vdoing | Copyright © 2023-2024 Easton Yang | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式