有很多关于Python和Ruby的讨论,我都发现它们完全没有帮助,因为它们都围绕着为什么X特性在Y语言中很糟糕,或者声称Y语言没有X,尽管事实上它有。我也确切地知道为什么我更喜欢Python,但这也是主观的,对任何人的选择都没有帮助,因为他们可能与我在开发方面的品味不同。

因此,客观地列出这些差异将是有趣的。所以没有“Python的lambdas很糟糕”。相反,解释Ruby的lambda能做而Python不能做的事情。没有主体性。示例代码很好!

请不要在一个答案上有几个不同之处。然后给你认为正确的选项投票,把你认为不正确的(或主观的)选项投下。此外,语法上的差异也不有趣。我们知道Python对缩进的处理就像Ruby对括号和结束的处理一样,@在Python中被称为self。

更新:现在这是一个社区维基,所以我们可以在这里添加大的区别。

Ruby在类主体中有一个类引用

在Ruby中,类主体中已经有了对类(self)的引用。在Python中,直到类构造完成之后才有对类的引用。

一个例子:

class Kaka
  puts self
end

self在本例中是类,这段代码将打印出“Kaka”。在Python中,无法打印类名或以其他方式从类定义体(在方法定义之外)访问类。

Ruby中所有的类都是可变的

这使您可以开发核心类的扩展。下面是一个rails扩展的例子:

class String
  def starts_with?(other)
    head = self[0, other.length]
    head == other
  end
end

Python(想象没有”。startswith方法):

def starts_with(s, prefix):
    return s[:len(prefix)] == prefix

你可以在任何序列上使用它(不仅仅是字符串)。为了使用它,你应该显式地导入它,例如,从some_module import starts_with。

Ruby具有类似perl的脚本功能

Ruby拥有一流的regexp、$-variables、awk/perl逐行输入循环和其他特性,这些特性使它更适合编写小型shell脚本,这些脚本可以转换文本文件或充当其他程序的粘合代码。

Ruby具有一流的延续

多亏了callcc语句。在Python中,您可以通过各种技术创建continuation,但语言中没有内置的支持。

Ruby有块

通过“do”语句,您可以在Ruby中创建一个多行匿名函数,该函数将作为参数传递到do前面的方法中,并从那里调用。在Python中,您可以通过传递方法或使用生成器来完成此操作。

Ruby:

amethod { |here|
    many=lines+of+code
    goes(here)
}

Python (Ruby块对应于Python中的不同构造):

with amethod() as here: # `amethod() is a context manager
    many=lines+of+code
    goes(here)

Or

for here in amethod(): # `amethod()` is an iterable
    many=lines+of+code
    goes(here)

Or

def function(here):
    many=lines+of+code
    goes(here)

amethod(function)     # `function` is a callback

有趣的是,在Ruby中调用块的方便语句称为“yield”,在Python中它将创建一个生成器。

Ruby:

def themethod
    yield 5
end

themethod do |foo|
    puts foo
end

Python:

def themethod():
    yield 5

for foo in themethod():
    print foo

尽管原理不同,结果却惊人地相似。

Ruby更容易支持函数式(类管道)编程

myList.map(&:description).reject(&:empty?).join("\n")

Python:

descriptions = (f.description() for f in mylist)
"\n".join(filter(len, descriptions))

Python有内置生成器(如上所述,像Ruby块一样使用)

Python支持该语言中的生成器。在Ruby 1.8中,您可以使用generator模块,该模块使用continuation从块中创建生成器。或者,你可以使用block/proc/lambda!此外,在Ruby 1.9中,光纤可以用作生成器,并且枚举器类是一个内置生成器4

Docs.python.org有这样一个生成器示例:

def reverse(data):
    for index in range(len(data)-1, -1, -1):
        yield data[index]

将其与上面的块示例进行对比。

Python具有灵活的名称空间处理

在Ruby中,当您使用require导入文件时,该文件中定义的所有内容都将在全局名称空间中结束。这会导致名称空间污染。解决方案就是ruby模块。但是如果使用模块创建名称空间,则必须使用该名称空间访问包含的类。

在Python中,该文件是一个模块,您可以从模块import *中导入其包含的名称,从而污染命名空间。但是你也可以从模块import aname中导入选定的名称,或者你可以简单地导入模块,然后使用module.aname访问这些名称。如果你想在你的命名空间中有更多的层,你可以有包,包是包含模块和__init__.py文件的目录。

Python有文档字符串

文档字符串是附加到模块、函数和方法的字符串 在运行时内省。这有助于创建帮助命令和 自动文档。

def frobnicate(bar):
    """frobnicate takes a bar and frobnicates it

       >>> bar = Bar()
       >>> bar.is_frobnicated()
       False
       >>> frobnicate(bar)
       >>> bar.is_frobnicated()
       True
    """

Ruby的等效程序类似于javadocs,并且位于方法的上方而不是内部。它们可以在运行时使用1.9的Method#source_location示例从文件中检索

Python具有多重继承

Ruby没有(“故意的”——请参阅Ruby的网站,了解在Ruby中是如何做到的)。它将模块概念重用为一种抽象类。

Python有列表/字典推导式

Python:

res = [x*x for x in range(1, 10)]

Ruby:

res = (0..9).map { |x| x * x }

Python:

>>> (x*x for x in range(10))
<generator object <genexpr> at 0xb7c1ccd4>
>>> list(_)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Ruby:

p = proc { |x| x * x }
(0..9).map(&p)

Python Python

>>> {x:str(y*y) for x,y in {1:2, 3:4}.items()}
{1: '4', 3: '16'}

Ruby:

>> Hash[{1=>2, 3=>4}.map{|x,y| [x,(y*y).to_s]}]
=> {1=>"4", 3=>"16"}

Python有装饰器

在Ruby中也可以创建类似于装饰器的东西,也可以认为它们不像在Python中那样必要。

语法差异

Ruby需要“end”或“}”来关闭所有作用域,而Python只使用空白。Ruby中最近已经尝试允许只使用空格缩进http://github.com/michaeledgar/seamless


当前回答

Python有一种“我们都是成年人”的心态。因此,您会发现Ruby有常量之类的东西,而Python没有(尽管Ruby的常量只会引发警告)。Python的思维方式是,如果你想让某些东西成为常数,你应该把变量名全大写,而不是改变它。

例如,Ruby:

>> PI = 3.14
=> 3.14
>> PI += 1
(irb):2: warning: already initialized constant PI
=> 4.14

Python:

>>> PI = 3.14
>>> PI += 1
>>> PI
4.1400000000000006

其他回答

Syntax is not a minor thing, it has a direct impact on how we think. It also has a direct effect on the rules we create for the systems we use. As an example we have the order of operations because of the way we write mathematical equations or sentences. The standard notation for mathematics allows people to read it more than one way and arrive at different answers given the same equation. If we had used prefix or postfix notation we would have created rules to distinguish what the numbers to be manipulated were rather than only having rules for the order in which to compute values.

标准符号使我们所讨论的数字一目了然,但计算它们的顺序却不明确。前缀和后缀表示法使计算顺序清晰明了,同时使数字模糊。如果不是语法空白造成的困难,Python已经有了多行lambda。(确实存在这样的建议,即不需要添加显式的块分隔符。)

I find it easier to write conditions where I want something to occur if a condition is false much easier to write with the unless statement in Ruby than the semantically equivalent "if-not" construction in Ruby or other languages for example. If most of the languages that people are using today are equal in power, how can the syntax of each language be considered a trivial thing? After specific features like blocks and inheritance mechanisms etc. syntax is the most important part of a language,hardly a superficial thing.

肤浅的是我们归于语法的美的审美品质。美学与我们的认知如何运作无关,但句法起作用。

Ruby有纹章和树枝,Python没有。

编辑:还有一件非常重要的事情我忘记了(毕竟,之前的只是燃烧一点点:-p):

Python有一个JIT编译器(Psyco),一种用于编写更快代码的低级语言(Pyrex),以及添加内联c++代码(Weave)的能力。

最终,所有的答案在某种程度上都是主观的,到目前为止发布的答案几乎证明了你不能以同样好的方式(如果不是相似的话)指出任何一个功能在另一种语言中是不可行的,因为这两种语言都非常简洁和富有表现力。

我喜欢Python的语法。但是,除了语法之外,您还必须深入挖掘Ruby的真正魅力。在林心如的一贯中有一种禅意般的美。虽然没有一个简单的例子可以完全解释这一点,但我会试着在这里举一个例子来解释我的意思。

颠倒这个字符串中的单词:

sentence = "backwards is sentence This"

当你考虑如何做到这一点时,你会这样做:

把这个句子分成几个单词 颠倒单词 将单词重新连接成字符串

在Ruby中,你可以这样做:

sentence.split.reverse.join ' '

正如您所想的那样,在相同的顺序中,一个方法调用另一个方法。

在python中,它看起来更像这样:

" ".join(reversed(sentence.split()))

这并不难理解,但它并没有完全相同的流程。主语(句)被埋没在中间。操作是函数和对象方法的混合。这是一个简单的例子,但是当你真正使用和理解Ruby时,你会发现许多不同的例子,特别是在非简单的任务中。

来自Ruby的网站:

相似之处 和Python一样,在Ruby中……

There’s an interactive prompt (called irb). You can read docs on the command line (with the ri command instead of pydoc). There are no special line terminators (except the usual newline). String literals can span multiple lines like Python’s triple-quoted strings. Brackets are for lists, and braces are for dicts (which, in Ruby, are called “hashes”). Arrays work the same (adding them makes one long array, but composing them like this a3 = [ a1, a2 ] gives you an array of arrays). Objects are strongly and dynamically typed. Everything is an object, and variables are just references to objects. Although the keywords are a bit different, exceptions work about the same. You’ve got embedded doc tools (Ruby’s is called rdoc).

差异 与Python不同,在Ruby中……

Strings are mutable. You can make constants (variables whose value you don’t intend to change). There are some enforced case-conventions (ex. class names start with a capital letter, variables start with a lowercase letter). There’s only one kind of list container (an Array), and it’s mutable. Double-quoted strings allow escape sequences (like \t) and a special “expression substitution” syntax (which allows you to insert the results of Ruby expressions directly into other strings without having to "add " + "strings " + "together"). Single-quoted strings are like Python’s r"raw strings". There are no “new style” and “old style” classes. Just one kind. You never directly access attributes. With Ruby, it’s all method calls. Parentheses for method calls are usually optional. There’s public, private, and protected to enforce access, instead of Python’s _voluntary_ underscore __convention__. “mixin’s” are used instead of multiple inheritance. You can add or modify the methods of built-in classes. Both languages let you open up and modify classes at any point, but Python prevents modification of built-ins — Ruby does not. You’ve got true and false instead of True and False (and nil instead of None). When tested for truth, only false and nil evaluate to a false value. Everything else is true (including 0, 0.0, "", and []). It’s elsif instead of elif. It’s require instead of import. Otherwise though, usage is the same. The usual-style comments on the line(s) above things (instead of docstrings below them) are used for generating docs. There are a number of shortcuts that, although give you more to remember, you quickly learn. They tend to make Ruby fun and very productive.

某人给出的嵌套词法范围示例有几个好处。

“更安全”的全局变量 这是将DSL嵌入到程序中的一种方法。

我认为这是两种语言之间差异的一个很好的例子。Ruby更加灵活。Python可以很灵活,但通常必须做一些极端的扭曲才能达到这一点,这使得它不值得麻烦。

抱歉没有在原来的答案下面发帖,我想我没有权限这么做。