关于dir()和getattr() python,该怎么解决
关于dir()和getattr() python
dir()没有参数时返回的是当前scope的表。
类似的我想用getattr()这样,第一个参数该填什么返回当前scope的属性值?
------解决方案--------------------
貌似也没有更好的办法...
vars()
locals()
globals()
------解决方案--------------------
貌似字典本来就在那,只是引用一下...
>>> import sys
>>> a = locals()
>>> sys.getrefcount(a)
5
>>> b = locals()
>>> sys.getrefcount(b)
6
>>> a is b
True
>>> c = {}
>>> sys.getrefcount(c)
2
>>> d = {}
>>> sys.getrefcount(d)
2
>>> c is d
False
>>>
dir()没有参数时返回的是当前scope的表。
类似的我想用getattr()这样,第一个参数该填什么返回当前scope的属性值?
------解决方案--------------------
貌似也没有更好的办法...
vars()
locals()
globals()
------解决方案--------------------
貌似字典本来就在那,只是引用一下...
>>> import sys
>>> a = locals()
>>> sys.getrefcount(a)
5
>>> b = locals()
>>> sys.getrefcount(b)
6
>>> a is b
True
>>> c = {}
>>> sys.getrefcount(c)
2
>>> d = {}
>>> sys.getrefcount(d)
2
>>> c is d
False
>>>