Python_API_Built-in Functions_globals

本文详细介绍了Python内置函数globals()的功能及使用方法。globals()用于返回当前全局符号表的字典,即当前模块的字典。文章通过具体示例展示了如何在Python程序中使用globals()来获取全局变量。

API文档:

globals()
Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).
翻译文档:

       描述:

            返回一个字典的。该字典中存储则全局的变量值。全局主要是当前的模块,包括导入到本地的模块例如:from sgmllib import SGMLParser那么SGMLParser中定义的变量就被添加到了globals的字典中。

例子:

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

aa='ccc'

from sgmllib import SGMLParser

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

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

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

输出:

9
10



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值