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

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

问题描述

在 Java 中 IoC/DI 是一种非常普遍的做法,广泛用于 Web 应用程序、几乎所有可用的框架和 Java EE.另一方面,也有很多大型 Python Web 应用程序,但除了 Zope(我听说它的编码应该非常糟糕)IoC 在 Python 世界中似乎并不常见.(如果你认为我错了,请举出一些例子).

当然有几种流行的 Java IoC 框架可用于 Python,例如 springpython.但它们似乎都没有得到实际使用.至少,我从来没有遇到过 Djangosqlalchemy+<在此处插入您最喜欢的 wsgi 工具包> 基于使用类似内容的 Web 应用程序.

在我看来,IoC 具有合理的优势,例如可以轻松替换 django-default-user-model,但是在 Python 中大量使用接口类和 IoC 看起来有点奇怪,而不是»pythonic«.但也许有人有更好的解释,为什么 IoC 在 Python 中没有被广泛使用.

解决方案

我实际上并不认为 DI/IoC 在 Python 中不常见.然而, 不常见的是 DI/IoC 框架/容器.

想一想:DI 容器有什么作用?它允许您

  1. 将独立的组件连接成一个完整的应用程序......
  2. ...在运行时.

我们有连接在一起"的名称;和在运行时":

  1. 脚本
  2. 动态

因此,DI 容器只不过是动态脚本语言的解释器.实际上,让我重新表述一下:典型的 Java/.NET DI 容器只不过是一个糟糕的解释器,用于处理非常糟糕的动态脚本语言,语法非常糟糕,有时是基于 XML 的语法.

当您使用 Python 编程时,当您拥有一种漂亮、出色的脚本语言可供使用时,为什么还要使用一种丑陋、糟糕的脚本语言?实际上,这是一个更普遍的问题:当您使用几乎任何语言进行编程时,当您可以使用 Jython 和 IronPython 时,为什么还要使用一种丑陋、糟糕的脚本语言?

所以,总结一下:DI/IoC 的实践在 Python 中与在 Java 中一样重要,原因完全相同.然而,DI/IoC 的实现是内置于语言中的,而且通常非常轻量级,以至于它完全消失了.

(这里有一个简短的类比:在汇编中,子程序调用是一件非常重要的事情 - 您必须将局部变量和寄存器保存到内存中,将您的返回地址保存在某处,将指令指针更改为您的子程序正在调用,安排它在完成后以某种方式跳回您的子例程,将参数放在被调用者可以找到它们的某个地方,等等.IOW:在汇编中,子例程调用"是一种设计模式,并且在此之前有像 Fortran 这样的语言内置了子例程调用,人们正在构建自己的子例程框架".你会说子例程调用在 Python 中不常见",只是因为你不使用子例程框架?)

顺便说一句:有关将 DI 推向其逻辑结论的示例,请查看 Gilad BrachaNewspeak Programming Language 和他关于这个主题的著作:

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).

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.

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.

解决方案

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

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

  1. wire together independent components into a complete application ...
  2. ... at runtime.

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

  1. scripting
  2. dynamic

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, sometimes XML-based, syntax.

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?

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.

(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: 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:

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

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