从Ruby学习Python;异同 [英] Learning Python from Ruby; Differences and Similarities

查看:38
本文介绍了从Ruby学习Python;异同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常了解 Ruby.我相信我现在可能需要学习 Python.对于两者都了解的人来说,两者在概念上有什么相似之处,又有什么不同之处?

I know Ruby very well. I believe that I may need to learn Python presently. For those who know both, what concepts are similar between the two, and what are different?

我正在寻找类似于我为为 JavaScript 用户学习 Lua 编写的入门书的列表: 简单的东西,比如空白意义和循环结构;nil 在 Python 中的名称,以及哪些值被认为是真实的";使用 mapeach 的等价物是惯用的,还是 mumble somethingaboutlistcomprehensions mumble 规范?

I'm looking for a list similar to a primer I wrote for Learning Lua for JavaScripters: simple things like whitespace significance and looping constructs; the name of nil in Python, and what values are considered "truthy"; is it idiomatic to use the equivalent of map and each, or are mumble somethingaboutlistcomprehensions mumble the norm?

如果我得到各种各样的答案,我很乐意将它们汇总到社区维基中.否则,你们都可以互相争斗,互相学习,试图创建一个真正的综合列表.

If I get a good variety of answers I'm happy to aggregate them into a community wiki. Or else you all can fight and crib from each other to try to create the one true comprehensive list.

编辑:明确地说,我的目标是正确的"和惯用的 Python.如果有一个 Python 等价于 inject,但没有人使用它,因为有更好/不同的方法来实现迭代列表和累积结果的通用功能,我想知道如何你做事.也许我会用一个常见目标列表更新这个问题,你如何在 Ruby 中实现它们,并询问 Python 中的等价物是什么.

Edit: To be clear, my goal is "proper" and idiomatic Python. If there is a Python equivalent of inject, but nobody uses it because there is a better/different way to achieve the common functionality of iterating a list and accumulating a result along the way, I want to know how you do things. Perhaps I'll update this question with a list of common goals, how you achieve them in Ruby, and ask what the equivalent is in Python.

推荐答案

以下是我认为的一些主要区别:

Here are some key differences to me:

  1. Ruby 有块;Python 没有.

  1. Ruby has blocks; Python does not.

Python 有函数;红宝石没有.在 Python 中,您可以将任何函数或方法传递给另一个函数.在Ruby中,一切都是方法,方法不能直接传递.相反,您必须将它们包装在 Proc 中才能传递它们.

Python has functions; Ruby does not. In Python, you can take any function or method and pass it to another function. In Ruby, everything is a method, and methods can't be directly passed. Instead, you have to wrap them in Proc's to pass them.

Ruby 和 Python 都支持闭包,但方式不同.在 Python 中,您可以在另一个函数中定义一个函数.内部函数对外部函数的变量有读访问权限,但没有写访问权限.在 Ruby 中,您可以使用块来定义闭包.闭包具有对外部作用域变量的完全读写访问权限.

Ruby and Python both support closures, but in different ways. In Python, you can define a function inside another function. The inner function has read access to variables from the outer function, but not write access. In Ruby, you define closures using blocks. The closures have full read and write access to variables from the outer scope.

Python 具有列表推导式,非常具有表现力.例如,如果你有一个数字列表,你可以写

Python has list comprehensions, which are pretty expressive. For example, if you have a list of numbers, you can write

[x*x for x in values if x > 15]

要获取所有大于 15 的值的平方的新列表.在 Ruby 中,您必须编写以下内容:

to get a new list of the squares of all values greater than 15. In Ruby, you'd have to write the following:

values.select {|v| v > 15}.map {|v| v * v}

Ruby 代码感觉没有那么紧凑.它也不是那么有效,因为它首先将值数组转换为包含大于 15 的值的较短的中间数组.然后,它采用中间数组并生成包含中间值平方的最终数组.然后将中间数组抛出.因此,Ruby 在计算过程中最终在内存中拥有 3 个数组;Python 只需要输入列表和结果列表.

The Ruby code doesn't feel as compact. It's also not as efficient since it first converts the values array into a shorter intermediate array containing the values greater than 15. Then, it takes the intermediate array and generates a final array containing the squares of the intermediates. The intermediate array is then thrown out. So, Ruby ends up with 3 arrays in memory during the computation; Python only needs the input list and the resulting list.

Python 也提供类似的地图理解.

Python also supplies similar map comprehensions.

Python 支持元组;鲁比没有.在 Ruby 中,您必须使用数组来模拟元组.

Python supports tuples; Ruby doesn't. In Ruby, you have to use arrays to simulate tuples.

Ruby 支持 switch/case 语句;Python 没有.

Ruby supports switch/case statements; Python does not.

Ruby 支持标准的 expr ?val1 : val2 三元运算符;Python 没有.

Ruby supports the standard expr ? val1 : val2 ternary operator; Python does not.

Ruby 仅支持单继承.如果您需要模拟多重继承,您可以定义模块并使用 mix-ins 将模块方法拉入类中.Python 支持多重继承而不是模块混合.

Ruby supports only single inheritance. If you need to mimic multiple inheritance, you can define modules and use mix-ins to pull the module methods into classes. Python supports multiple inheritance rather than module mix-ins.

Python 仅支持单行 lambda 函数.Ruby 块是一种/某种 lambda 函数,可以任意大.正因为如此,Ruby 代码通常以比 Python 代码更具功能性的风格编写.例如,要在 Ruby 中遍历列表,您通常会执行

Python supports only single-line lambda functions. Ruby blocks, which are kind of/sort of lambda functions, can be arbitrarily big. Because of this, Ruby code is typically written in a more functional style than Python code. For example, to loop over a list in Ruby, you typically do

collection.each do |value|
  ...
end

块的工作方式非常类似于传递给 collection.each 的函数.如果你要在 Python 中做同样的事情,你必须定义一个命名的内部函数,然后将它传递给每个方法的集合(如果列表支持这个方法):

The block works very much like a function being passed to collection.each. If you were to do the same thing in Python, you'd have to define a named inner function and then pass that to the collection each method (if list supported this method):

def some_operation(value):
  ...

collection.each(some_operation)

那不是很好地流动.因此,通常会在 Python 中使用以下非函数式方法:

That doesn't flow very nicely. So, typically the following non-functional approach would be used in Python:

for value in collection:
  ...

  • 以安全的方式使用资源在两种语言之间是完全不同的.这里的问题是你想分配一些资源(打开文件,获取数据库游标等),对其进行一些任意操作,然后即使发生异常也以安全的方式关闭它.

  • Using resources in a safe way is quite different between the two languages. Here, the problem is that you want to allocate some resource (open a file, obtain a database cursor, etc), perform some arbitrary operation on it, and then close it in a safe manner even if an exception occurs.

    在 Ruby 中,由于块非常易于使用(参见 #9),您通常将此模式编码为一种方法,该方法采用块来对资源执行任意操作.

    In Ruby, because blocks are so easy to use (see #9), you would typically code this pattern as a method that takes a block for the arbitrary operation to perform on the resource.

    在 Python 中,为任意操作传入函数有点笨拙,因为您必须编写一个命名的内部函数(参见 #9).相反,Python 使用 with 语句来进行安全的资源处理.请参阅如何正确清理 Python 对象? 了解更多详情.

    In Python, passing in a function for the arbitrary action is a little clunkier since you have to write a named, inner function (see #9). Instead, Python uses a with statement for safe resource handling. See How do I correctly clean up a Python object? for more details.

    这篇关于从Ruby学习Python;异同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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