Python_API_Built-in Functions_locals

本文介绍了Python内置函数locals()的功能及用法。locals()用于获取并返回一个表示当前局部符号表的字典,其中包括函数参数等变量。文章通过实例演示了如何在函数中使用locals()来查看局部变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

API文档:

locals()

      Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks.

     Note

         The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.

翻译文档:

       描述:

             这个方法返回一个字典。该字典中存储局部名字空间(当前函数或是类方法)的变量,包括函数的参数。

例子:

#! /usr/bin/env python
#coding=utf-8

aa='ccc'

class TestC:
    b = 'bb'
    
    def f(self,x):
        print locals()

def fun(param):
    x = 0
    print locals()
    
fun('cc')

t = TestC()
t.f('g')

输出:

{'x': 0, 'param': 'cc'}
{'x': 'g', 'self': <__main__.TestC instance at 0x01AC8AA8>}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值