为什么的IoC / DI Python中不常见? [英] Why is IoC / DI not common in Python?

查看:3615
本文介绍了为什么的IoC / DI Python中不常见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java的的IoC /的 DI 是一个非常普遍的做法是广泛的Web应用中使用,几乎所有的可用的框架和Java EE。而另一方面,也有很多大的Python的Web应用程序,但Zope的(这我听说应该是真正可怕code)对旁边的IoC似乎不是很常见的Python世界。 (请列举一些例子,如果你认为我错了)。

In Java IoC / DI is a very common practice which is extensively used in web applications, nearly all available frameworks and Java EE. On the other hand, there are also lots of big Python web applications, but beside of Zope (which I've heard should be really horrible to code) IoC doesn't seem to be very common in the Python world. (Please name some examples if you think that I'm wrong).

当然也有可用于Python, springpython 例如流行的Java的IoC框架的几个克隆。但没有人似乎得到实际应用。至少,我从来没有在一个 Django的 stumpled SQLAlchem​​y的 + <此处&GT插入您最喜爱的WSGI工具包; 它使用类似的东西,基于Web的应用程序

There are of course several clones of popular Java IoC frameworks available for Python, springpython for example. But none of them seems to get used practically. At least, I've never stumpled upon a Django or sqlalchemy+<insert your favorite wsgi toolkit here> based web application which uses something like that.

在我看来国际奥委会合理的优势,将可以很容易地更换Django的默认用户模式的例子,但在Python接口类和IOC的广泛使用看起来有点古怪,而不是Python的»«。但也许有人有一个更好的解释,为什么国际奥委会没有被广泛使用Python编写的。

In my opinion IoC has reasonable advantages and would make it easy to replace the django-default-user-model for example, but extensive usage of interface classes and IoC in Python looks a bit odd and not »pythonic«. But maybe someone has a better explanation, why IoC isn't widely used in Python.

推荐答案

我真的不认为DI /国际奥委会在Python中的的少见。什么的的少见,然而,DI /国际奥委会的框架/容器

I don't actually think that DI/IoC are that uncommon in Python. What is uncommon, however, are DI/IoC frameworks/containers.

想想看:请问一个DI容器吗?它可以让你

Think about it: what does a DI container do? It allows you to


  1. 丝一起独立的组件集成到一个完整的应用...

  2. ...在运行时。

我们有在运行时为接线合和名称:

We have names for "wiring together" and "at runtime":


  1. 脚本

  2. 动态

所以,DI容器也不过是一个跨preTER的动态脚本语言。其实,让我换一种说法:一个典型的Java / .NET DI容器不过是一个蹩脚的跨preTER与对接丑,往往基于XML的,语法一个非常糟糕的动态脚本语言

So, a DI container is nothing but an interpreter for a dynamic scripting language. Actually, let me rephrase that: a typical Java/.NET DI container is nothing but a crappy interpreter for a really bad dynamic scripting language with butt-ugly, often XML-based, syntax.

当您在Python程序,为什么你想,当你有在您的处置美好,辉煌的脚本语言使用一个丑陋的,坏的脚本语言?其实,这是一个更普遍的问题:当你在pretty太多的语言程序,为什么你想,当你有Jython和IronPython的使用一个丑陋的,坏的脚本语言在您的处置

When you program in Python, why would you want to use an ugly, bad scripting language when you have a beautiful, brilliant scripting language at your disposal? Actually, that's a more general question: when you program in pretty much any language, why would you want to use an ugly, bad scripting language when you have Jython and IronPython at your disposal?

因此​​,要回顾一下:在实践的DI的/ IoC的仅仅是在Python一样重要,因为它是在Java中,对于完全相同的原因。在实施的不过DI /国际奥委会的,内置的语言,往往轻量型的,它完全消失。

So, to recap: the practice of DI/IoC is just as important in Python as it is in Java, for exactly the same reasons. The implementation of DI/IoC however, is built into the language and often so lightweight that it completely vanishes.

(这里有一个简短的预留一个比喻:在组装,子程序调用是pretty主要的交易 - 你有你的局部变量和寄存器保存到内存中,保存在某个地方你的返回地址,更改指令指针您正在呼叫,安排它完成时以某种方式跳回你的子程序子程序,将这些参数的地方在那里被调用者可以找到他们,等IOW:在装配子程序调​​用是一种设计模式,之前有类似的Fortran语言其中有建于子程序调用,人们建立自己的子程序的框架。你说,子程序调用是罕见在Python,只是因为你不使用子程序框架吗?)

(Here's a brief aside for an analogy: in assembly, a subroutine call is a pretty major deal - you have to save your local variables and registers to memory, save your return address somewhere, change the instruction pointer to the subroutine you are calling, arrange for it to somehow jump back into your subroutine when it is finished, put the arguments somewhere where the callee can find them, and so on. IOW: in assembly, "subroutine call" is a Design Pattern, and before there were languages like Fortran which had subroutine calls built in, people were building their own "subroutine frameworks". Would you say that subroutine calls are "uncommon" in Python, just because you don't use subroutine frameworks?)

BTW:对于什么样子采取DI得出的逻辑结论的例子,看看吉拉德的Bracha 新话编程语言和他的著作主题:

BTW: for an example of what it looks like to take DI to its logical conclusion, take a look at Gilad Bracha's Newspeak Programming Language and his writings on the subject:

  • Constructors Considered Harmful
  • Lethal Injection
  • A Ban on Imports (continued)

这篇关于为什么的IoC / DI Python中不常见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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