python中globals用法_在Python中使用globals()的原因?

在“声明性python”中很有用。 例如,在下面的globals()和globals()中,是用于定义一系列数据结构的类,这些数据结构随后将被某些程序包用作其输入或配置。 这使您可以灵活地输入什么内容,并且不需要编写解析器。

# FooDef, BarDef are classes

Foo_one = FooDef("This one", opt1 = False, valence = 3 )

Foo_two = FooDef("The other one", valence = 6, parent = Foo_one )

namelist = []

for i in range(6):

namelist.append("nm%03d"%i)

Foo_other = FooDef("a third one", string_list = namelist )

Bar_thing = BarDef( (Foo_one, Foo_two), method = 'depth-first')

请注意,此配置文件使用循环来构建名称列表,这些列表是globals()的配置的一部分。因此,此配置语言带有功能非常强大的“预处理器”以及可用的运行时库。 如果您想查找复杂的日志,或从zip文件中提取内容并对其进行base64解码,以作为生成配置的一部分(当然,对于输入可能来自于 不可靠的来源...)

该软件包使用以下内容读取配置:

conf_globals = {} # make a namespace

# Give the config file the classes it needs

conf_globals['FooDef']= mypkgconfig.FooDef # both of these are based ...

conf_globals['BarDef']= mypkgconfig.BarDef # ... on .DefBase

fname = "user.conf"

try:

exec open(fname) in conf_globals

except Exception:

...as needed...

# now find all the definitions in there

# (I'm assuming the names they are defined with are

# significant to interpreting the data; so they

# are stored under those keys here).

defs = {}

for nm,val in conf_globals.items():

if isinstance(val,mypkgconfig.DefBase):

defs[nm] = val

因此,最后要讲到globals()很有用,如果要使用程序定义一系列定义,则在使用这样的软件包时:

for idx in range(20):

varname = "Foo_%02d" % i

globals()[varname]= FooDef("one of several", id_code = i+1, scale_ratio = 2**i)

这相当于写出来

Foo_00 = FooDef("one of several", id_code = 1, scale_ratio=1)

Foo_01 = FooDef("one of several", id_code = 2, scale_ratio=2)

Foo_02 = FooDef("one of several", id_code = 3, scale_ratio=4)

... 17 more ...

通过从python模块收集一堆定义来获取输入的软件包的示例是PLY(Python-lex-yacc)[http://www.dabeaz.com/ply/],在这种情况下,对象是 大多数是函数对象,但是来自函数对象的元数据(它们的名称,文档字符串和定义顺序)也构成了输入的一部分。 使用globals()并不是一个很好的例子。 而且,它是通过“配置”(后者是普通的python脚本)导入的,而不是相反的方式。

我在一些我从事过的项目上使用了“声明性python”,并且在为这些项目编写配置时曾使用globals()。 您当然可以辩解说,这是由于配置“语言”的设计方式存在缺陷。 以这种方式使用globals()不会产生非常清晰的结果。 只是结果,可能比写出十几个几乎完全相同的语句更容易维护。

您还可以根据变量的名称使用它在配置文件中赋予变量重要性:

# All variables above here starting with Foo_k_ are collected

# in Bar_klist

#

foo_k = [ v for k,v in globals().items() if k.startswith('Foo_k_')]

Bar_klist = BarDef( foo_k , method = "kset")

对于定义了许多表和结构的任何python模块,此方法可能很有用,从而可以更轻松地向数据中添加项目,而无需维护引用。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值