如果 Ruby 和 Python 都允许使用猴子补丁,为什么在 Ruby 中它更具争议? [英] If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?

查看:44
本文介绍了如果 Ruby 和 Python 都允许使用猴子补丁,为什么在 Ruby 中它更具争议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我听说过很多关于 Ruby 的讨论中,人们对语言表达了他们的保留意见,猴子补丁问题成为他们主要关注的问题之一.

In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary concerns.

然而,我很少听到在 Python 上下文中提出的相同论点,尽管它在 Python 语言中也是允许的.

However, I rarely hear the same arguments made in the context of Python although it is also permitted in the Python language.

为什么会有这种区别?

Python 是否包含不同类型的保护措施来最小化此功能的风险?

Does Python include different types of safeguards to minimize the risks of this feature?

推荐答案

作为一名尝过 Ruby(并喜欢它)的 Python 程序员,我认为 Python 开始成为很受欢迎.

As a Python programmer who has had a taste of Ruby (and likes it), I think there is somewhat of an ironic parallel to when Python was beginning to become popular.

C 和 Java 程序员会抨击"Python,声称它不是真正的语言,并且其类型的动态性质是危险的,并允许人们创建坏"代码.随着 Python 变得越来越流行,其快速开发时间的优势变得明显,更不用说不那么冗长的语法了:

C and Java programmers would ‘bash’ Python, stating that it wasn't a real language, and that the dynamic nature of its types would be dangerous, and allow people to create ‘bad’ code. As Python became more popular, and the advantages of its rapid development time became apparent, not to mention the less verbose syntax:

// Java
Person p = new Person();

# Python
p = Person()

我们开始看到一些更动态的特性出现在更高版本的 Java 中.自动装箱和取消装箱使处理原语变得不那么麻烦,而泛型允许我们编写一次代码并将其应用于多种类型.

we began to see some more dynamic features appear in later versions of Java. Autoboxing and -unboxing make it less troublesome to deal with primitives, and Generics allow us to code once and apply it to many types.

我看到 Ruby 的关键灵活特性之一 – Monkey Patching 感到很有趣,它被 Python 人群吹捧为危险.今年开始向学生教授 Ruby 后,我认为能够修复"现有类的实现,即使是系统的一部分,也是非常强大的.

It was with some amusement that I saw one of the key flexible features of Ruby – Monkey Patching, being touted as dangerous by the Python crowd. Having started teaching Ruby to students this year, I think that being able to ‘fix’ the implementation of an existing class, even one that is part of the system, is very powerful.

当然,您可能会搞砸并导致程序崩溃.我也可以很容易地在 C 中出现段错误.而且 Java 应用程序可能会死而复生.

Sure, you can screw up badly and your program can crash. I can segfault in C pretty easily, too. And Java apps can die flaming death.

事实是,我认为 Monkey Patching 是动态和元编程的下一步.有趣,因为它从 Smalltalk 开始就存在了.

The truth is, I see Monkey Patching as the next step in dynamic and meta-programming. Funny, since it has been around since Smalltalk.

这篇关于如果 Ruby 和 Python 都允许使用猴子补丁,为什么在 Ruby 中它更具争议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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