Python 2.5 中的类装饰器?
有没有办法让类装饰器在仅限于Python 2.5
的Google App Engine上工作?
或者让我重新表述一下:是否可以从 Python 解析器已经执行的同一进程中改变它的行为?示例:
good.py:
alter_python_parser()
import bad
bad.py:
@decorated
class Foo(object): pass
或者这可能根本不可能。
说明:我想使用大量使用类装饰器的第三方库,并且不想分叉它并维护我自己的版本。另一种方法是使用较新的 Python 在 Typhoon App Engine
上运行我的代码,但我担心 Google 不会在很长一段时间内升级他们的 Python 版本...
< strong>编辑:
创建一个新的怎么样-style import hook 可以即时进行字符串替换并从内存加载模块?那应该是可能的。如果还没有实现,我会尝试一下。
但是如何从 Python 2.5
解析 Python 2.6+
代码呢?有没有只支持 python 的解析器? PYPY
使用什么?
Is there a way I can make class decorators work on Google App Engine, which is limited to Python 2.5
?
Or let me rephrase that: is it possible to alter the behavior of Python's parser from the same process that it is already executing? Example:
good.py:
alter_python_parser()
import bad
bad.py:
@decorated
class Foo(object): pass
Or is this maybe just plainly impossible.
Explanation: I want to use a third party library that heavily uses class decorators, and don't want to fork it and maintain my own version. An alternative would be to run my code on Typhoon App Engine
with a newer python, but I fear Google won't upgrade their Python version for a loooong time...
EDIT:
How about creating a new-style import hook that would do string substitution on-the-fly and load the module from memory? That should be possible. I'll give it a try, if there's no implementation already out there.
But how can I parse Python 2.6+
code from Python 2.5
? Is there a python-only parser? What does PYPY
use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
装饰器只是语法糖。只需更改装饰器使用的实例,即
变为
您实际上无法更改解析器。
不过,您可以使用 ast 模块 (在新版本中)自动执行上述过程Python)。
Decorators are just syntactic sugar. Just change instances of decorator usage, that is,
becomes
You can't, realistically, change the parser.
Though, you could automate the above process using the ast module (in a new version of Python).