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

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

问题描述

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



当然有几个可用于Python的Java IoC框架的克隆, springpython 。但是他们似乎都没有被实际使用。至少,我从来没有在 Django sqlalchemy + <在这里插入您最喜欢的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的时候,为什么要使用丑陋的脚本语言?


$ b $因此,要概述:DI / IoC的练习与Python中的Java一样重要,因为完全相同的原因。然而,DI / IoC的实现内置于语言中,通常非常轻巧,完全消失。



(这里简单介绍一下:在汇编中,一个子程序调用是一个很大的交易 - 你必须将你的本地变量和寄存器保存到内存中,保存返回地址在某处,将指令指针更改为您调用的子例程,安排它以某种方式跳回到子程序完成后,将参数放在被叫方可以找到的地方,等等。IOW:在汇编中, 子程序调用是一种设计模式,而在有内置子例程调用的Fortran语言之前,人们正在构建自己的子程序框架,你可以说这个子程序调用在Python中是不常见的,只是因为你不要使用子例程框架?)



BTW:举个例子,看看DI的逻辑结论,看看 Gilad Bracha Newspeak Progra mming语言及其关于这个主题的着作:




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, often 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天全站免登陆