栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中的locals内置函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中的locals内置函数

python中的locals内置函数 1. locals的源码
def locals() -> dict[str, Any]: ...
(function) locals: () -> dict[str, Any]
Return a dictionary containing the current scope's local variables.

NOTE: Whether or not updates to this dictionary will affect name lookups in the local scope and vice-versa is *implementation dependent* and not covered by any backwards compatibility guarantees.

注:locals的功能是将局部变量打包成一个字典

2. 实例
def test(a, b):
	alist = [1, 2, 3, 4]
	dic = {"age": 12}
	print(locals())

# 最终的打印结果为{'a': 1, 'b': 2, 'alist': [1, 2, 3, 4], 'dic': {'age': 18}}
3. 使用场景

我是在学习django的模板渲染render函数中,接触到的locals内置函数,可以很方便地将局部变量打包成字典,通过**render(request, template_html, content)**中的content传递字典数据

参考链接:https://www.runoob.com/python/python-func-locals.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317316.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号