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

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

问题描述

可能的重复:
任何适用于 Python 的 AOP 支持库?

我熟悉 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 本身的动态机制.谷歌搜索会产生几个项目 - 尽管看起来只是库,但它是 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.

这不是我编造的——事实上,您可以内省类和方法,并在运行时更改它们.当我第一次了解面向方面时,我可以在几个小时内用 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语言​​扩展"可以用于面向方面:当我进行上面提到的概念证明时,我曾经在运行时检查方法的输入参数确定某些方法是否会受到规则的影响.

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.

顺便说一句,这里是我的快速技巧,以获取使用纯 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天全站免登陆