全局范围内的描述符?
Python 2.6 中的 描述符协议 仅定义为类定义,因此只能由实例使用。
是否有一些等效的工具来检测全局变量的获取/设置?
我正在尝试加快与主机系统交互的模块的导入速度,因此必须对主机执行一些昂贵的探测。 (昂贵的)探测的结果存储在导入时初始化的模块全局中;所以我试图推迟初始化直到绝对需要。
请不要评论全局变量是邪恶的。我知道它们是什么以及何时使用它们。
我当前的计划是创建一个使用描述符的全局实例,并将当前的所有全局变量移至该实例的属性中。我希望这会起作用;我只是想问一下还有没有别的办法
The descriptor protocol in Python 2.6 is only defined for class definitions, and thus can only be used by instances.
Is there some equivalent for instrumenting get/set of globals?
I'm trying to speed up the importing of a module which interacts with the host system, and as such has to perform some expensive probing of the host. The results of the (expensive) probe are stored in a module global that is initialized at import time; so I'm trying to delay the initialization until absolutely required.
Please, no comments about globals being evil. I know what they are and when to use them.
My current plan is to create a global instance that uses descriptors, and move all my current globals into the attributes of this instance. I expect this will work; I'm just asking if there's another way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这正是我要做的。类之外没有与描述符等效的东西。
我有时也使用的另一个选项是使用函数而不是变量名,如下所示:
如果您已经在某处定义了
@memoize
装饰器,则可以简化上述内容相当。That's precisely what I would do. There is no equivalent to descriptors outside of classes.
The other option, which I have also sometimes used, would be to use a function instead of a variable name, something like this:
If you already have a
@memoize
decorator defined somewhere, you can simplify the above considerably.如果您确实想这样做,此链接提供了一个非常酷的实现方法。唯一需要注意的是您必须使用 eval/exec/execfile 执行代码。
https://mail.python.org/pipermail/python-ideas /2011-March/009657.html
虽然有点麻烦,但我认为这非常酷。
If you really want to do this, this link gives a pretty cool method to implement. The only caveat is you'd have to execute your code using eval/exec/execfile.
https://mail.python.org/pipermail/python-ideas/2011-March/009657.html
While a little cumbersome, I thought this was very cool.