Python中的面向方面的编程(AOP) [英] Aspect oriented programming (AOP) in Python

查看:130
本文介绍了Python中的面向方面的编程(AOP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
是否有适用于Python的AOP支持库?

Possible Duplicate:
Any AOP support library for Python?

我熟悉Java语言的AspectJ扩展名.

I am familiar with the AspectJ extension for the Java language.

我想知道Python是否有这样的事情.

I want to know if there is such a thing for Python.

不要误会我的意思,我不是说图书馆,而是像AspectJ这样的语言扩展是Java.

Don't get me wrong, I do not mean a library but a language extension like AspectJ is to Java.

推荐答案

Python不需要像语言扩展"之类的东西就可以以面向方面的方式工作.

Python does not need something like a "language extension" for being able to work in an Aspect Oriented way.

这仅仅是由于Python本身的动态机制. Google搜索将产生几个项目-但尽管看起来只像库,但这是Python所需要的.

That is simply due to the dynamic mechanisms in Python itself. A Google search will yield a couple projects - but despite looking merely like libraries, it is all that is needed in Python.

我没有弄错-这是您可以自省类和方法并在运行时更改它们的事实.当我第一次了解Aspect Orientation时,我可以在几个小时内用Python实现一些概念验证-当然,某些现有项目可以提供生产质量的条目.

I am not making this up - it is the fact that you can introspect classes and methods, and change them at run-time. When I first learned about Aspect Orientation, I could implement some proof of concepts in Python in a couple of hours - certainly some of the existing projects can offer production-quality entries.

但是,正如您所问的那样,有一种Python的语言扩展"可以用于Aspect Orientation:当我进行上面提到的概念验证时,我曾经在运行时检查方法的输入参数确定某些方法是否会受到规则的影响.

But since you asked, there is a Python "language extension" of sorts that could be used for Aspect Orientation: when I made the proof of concept I mentioned above, I used to check the input parameters to methods at run-time to determine whether certain methods would be affected by a rule or not.

在Python 3中,该语言有一个鲜为人知的功能,它允许人们注释输入参数以及函数或方法的返回值.方面方向库可以利用此库在加载时间"而不是在每个函数调用时应用其魔力.

In Python 3 there is a little known feature of the language that allows one to annotate the input parameters and return value of a function or method. An aspect orientation library could make use of this to apply its magic at "load time", and not at the time of each function call.

顺便说一句,这是我的快速技巧,以获取将Aspect Orientation与Pure Python结合使用的有效示例.抱歉-代码注释在pt_BR中- https://bitbucket.org/jsbueno/metapython/src/f48d6bd388fd/aspect.py

BTW, here is my quick hack to get a working example of using Aspect Orientation with Pure Python. Sorry - the code comments are in pt_BR - https://bitbucket.org/jsbueno/metapython/src/f48d6bd388fd/aspect.py

这篇关于Python中的面向方面的编程(AOP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆