为什么我选择Python而不是Ruby [英] Why I chose Python over Ruby

查看:53
本文介绍了为什么我选择Python而不是Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前我发现了Python,并很快决定将时间花在

上。在网上浏览Python时,我还看到了超过Ruby的炒作,并试图找到更多关于它的信息,之后我肯定会开始学习和练习Python。我最近发现两个

足够的答案来选择Python - 这是个人选择和

其他人可能会有所不同,但无论如何我想分享它:


1)在Ruby中存在变量/方法模糊的风险。在没有使用()的情况下调用

a方法时没有参数:


这是摘自Programming Ruby The Pragmatic的书摘录
程序员指南。

http ://www.rubycentral.com/book/language.html


"当Ruby在表达式中看到诸如a的名称时,它需要

确定它是一个局部变量引用还是一个没有参数的方法调用

。为了确定是哪种情况,Ruby使用了一种启发式方法。

当Ruby读取源文件时,它会跟踪分配给它的符号。它假定这些符号是变量。当它随后出现一个可能是变量或

方法调用的符号时,它会检查它是否已经看到之前的分配

符号。如果是这样,它将符号视为变量;否则它将

视为方法调用。作为一个有点病态的案例,请考虑以下代码片段,由Clemens Hintze提交。


def a

打印功能'a''叫做'\\ n'"

99

结束


for i in 1。 .2

if i == 2

print" a =",a," \ n"

else

a = 1

print" a =",a," \ n"

end

end


输出>>


a = 1

功能''a''名为

a = 99


"在解析过程中,Ruby在第一个打印

语句中看到了``a'''的使用,因为它还没有看到对a的任何赋值,'''假设

它是一个方法调用。当它到达第二个打印

语句时,它已经看到了一个赋值,因此将a作为

变量。

注意,不必执行赋值--- Ruby只有

才能看到它。这个程序不会引发错误。


我在交互式Ruby 1.8.2解释器上尝试了上面的代码:

http://www.ruby.ch/tutorial/


2)Ruby没有与其他变量一样生活在同一个

名称空间中的真正的第一类函数,而Python则是:


在Python中:


def sayHello(姓名):

return" Hello" +名字

打印sayHello(Mr。Bond)

m = sayHello

print m

print m (Moneypenny小姐)


输出>>


你好先生邦德

<函数sayHello at 0x0102E870>

Hello Miss Moneypenny


在Ruby中你需要额外的语法来破坏一流的。 :


def sayHello(姓名)

return" Hello" +名字

结束

把sayHello(Mr。Bond)

m = Class.method(:sayHello)

put m

put m.call(Miss Moneypenny)


输出>>


你好先生邦德

#<方法:上课(对象)#sayHello>

你好Moneypenny小姐


4)结论


由于我在Scheme中做了很多工作,严谨性和一致性对我来说非常重要,而且Python肯定符合这个要求。 br />

--- Python新手

解决方案

Francois写道:

我几个月前发现了Python,并很快决定投入时间来学习它。在浏览Python的网络时,我还看到了对Ruby的炒作,并试图找到更多关于它的信息,然后我才开始学习和练习Python。我最近找到了两个足够的答案来选择Python - 这是个人选择,其他可能不同,但我想分享它
无论如何




感谢分享。我有一种直觉,认为Python比Ruby更好,但我从未花时间研究Ruby。现在我确实有一些好的石头扔给Ruby-fanatics ;-)


Sybren


< blockquote> Francois< fl ****** @ hotmail.com>写道:

由于我在Scheme中做了很多工作,严谨性和一致性对我来说非常重要,而且Python当然也符合这个要求。




它确实很好,有一些实用主义的磨练 - 但是,为了扮演恶魔的倡导者,Ruby并不是这方面的。在任何一种情况下,你都会发现更好的语言和更好的语言,比如Haskell更好的语言,但是肯定会找到很多。 />
Ruby或Python。

根据编译器看到的赋值是否为
来区分名称的确切性质的诀窍代码的某些部分中的名称是两种语言中的
,尽管方式不同。在Ruby中,正如你所指出的那样,它是用于消除零参数方法调用的局部变量

访问的启发式算法,以及部分代码是访问点的

功能。在Python中,它用于消除全局或自由变量的本地消息,以及代码的一部分。是整个函数的主体

(Ruby不需要将后者作为
的区别,因为它用一个主要的sigil划分全局名称 -


a是

总是全局变量a,就像@a总是实例

属性a一样,我们写的是self.a in蟒蛇)。

Ruby中的另一个微妙案例是,a_23 _with a block_的赋值是否意味着

是该块的本地或意图重新绑定本地名称''a ''

封闭函数;再次,启发式是相似的,仅取决于

编译器是否在看到

块之前看到了另一个赋值(Python禁止重新绑定来自的变量

附上但非全局范围,以避免面临这个问题。


总而言之,Python更有可能通过这些启发式来尊重

原则(来自zen of python,在交互式

提示下导入):面对模棱两可,拒绝猜测的诱惑;错误

不应该默默地传递,除非明确地沉默。即,任何Python

代码意外地与这些启发式方法发生冲突,可能会引发异常,提醒您注意这种情况。 Ruby为做我的意思而努力争取
。精神,显然有些人更喜欢这个。


我也分享你对可调用的单一命名空间和

不可调用值的偏好,如Python(和Scheme,Lisp,C ++,...),相比

比在Ruby(和Smalltalk)中的脱节命名空间,但是我没有看到

作为一个问题严格和一致性 - 例如,我不认为

Smalltalk与C ++相比不那么严谨或一致。


所以,我同意根据你的选择,我想我理解你的动机,

但是我个人并不完全分享你的动机。

Alex


I discovered Python a few months ago and soon decided to invest time in
learning it well. While surfing the net for Python, I also saw the hype
over Ruby and tried to find out more about it, before I definitely
embarked on studying and practicing Python. I recently found two
sufficient answers for choosing Python - which is a personal choice and
others may differ, but I''d like to share it anyway :

1) In Ruby there is a risk of "Variable/Method Ambiguity" when calling
a method with no parameters without using () :

Here is an excerpt from the book "Programming Ruby The Pragmatic
Programmer''s Guide".

http://www.rubycentral.com/book/language.html

"When Ruby sees a name such as ``a'''' in an expression, it needs to
determine if it is a local variable reference or a call to a method
with no parameters. To decide which is the case, Ruby uses a heuristic.
As Ruby reads a source file, it keeps track of symbols that have been
assigned to. It assumes that these symbols are variables. When it
subsequently comes across a symbol that might be either a variable or a
method call, it checks to see if it has seen a prior assignment to that
symbol. If so, it treats the symbol as a variable; otherwise it treats
it as a method call. As a somewhat pathological case of this, consider
the following code fragment, submitted by Clemens Hintze."

def a
print "Function ''a'' called\n"
99
end

for i in 1..2
if i == 2
print "a=", a, "\n"
else
a = 1
print "a=", a, "\n"
end
end

OUTPUTS >>

a=1
Function ''a'' called
a=99

"During the parse, Ruby sees the use of ``a'''' in the first print
statement and, as it hasn''t yet seen any assignment to ``a,'''' assumes
that it is a method call. By the time it gets to the second print
statement, though, it has seen an assignment, and so treats ``a'''' as a
variable.
Note that the assignment does not have to be executed---Ruby just has
to have seen it. This program does not raise an error."

I tried the code above at the interactive Ruby 1.8.2 interpreter :

http://www.ruby.ch/tutorial/

2) Ruby does not have true first-class functions living in the same
namespace as other variables while Python does :

In Python :

def sayHello (name) :
return "Hello " + name
print sayHello("Mr. Bond")
m = sayHello
print m
print m("Miss Moneypenny")

OUTPUTS >>

Hello Mr. Bond
<function sayHello at 0x0102E870>
Hello Miss Moneypenny

In Ruby you need extra syntax that ruins the "first-class-ness" :

def sayHello (name)
return "Hello " + name
end
puts sayHello("Mr. Bond")
m = Class.method(:sayHello)
puts m
puts m.call("Miss Moneypenny")

OUTPUTS >>

Hello Mr. Bond
#<Method: Class(Object)#sayHello>
Hello Miss Moneypenny

4) Conclusion

Since I did a lot of work in Scheme, rigor and consistency are most
important to me, and Python certainly meets this requirement.

--- Python newbie

解决方案

Francois wrote:

I discovered Python a few months ago and soon decided to invest time
in learning it well. While surfing the net for Python, I also saw
the hype over Ruby and tried to find out more about it, before I
definitely embarked on studying and practicing Python. I recently
found two sufficient answers for choosing Python - which is a
personal choice and others may differ, but I''d like to share it
anyway



Thanks for sharing that. I had a gut feeling Python would be better
than Ruby, but I never took the time to study Ruby. Now I do have some
nice stones to throw at Ruby-fanatics ;-)

Sybren


Francois <fl******@hotmail.com> wrote:

Since I did a lot of work in Scheme, rigor and consistency are most
important to me, and Python certainly meets this requirement.



It does pretty well, with some tempering of pragmatism -- but, to play
devil''s advocate, Ruby isn''t far in this respect. In either case, you
will find more rigor and consistency in good languages of a more
academic bent, such as Haskell, but will surely find a lot in either
Ruby or Python.

The trick about distinguishing a name''s exact nature based on whether
the compiler sees an assignment to that name in some part of code is
found in both languages, albeit in different ways. In Ruby, as you''ve
pointed out, it''s the heuristic used to disambiguate local variable
access from zero-argument method calls, and the "part of code" is the
function up to the point of access. In Python, it''s used to disambiguate
local from global or free variables, and the "part of code" is the body
of the whole function (Ruby does not need to make this latter
distinction because it strops global names with a leading sigil --


a is
always the global variable a, just like @a is always the instance
attribute a, which we''d write self.a in Python). Another subtle case in
Ruby is whether an assignment such as a=23 _within a block_ is meant to
be local to the block or meant to rebind local name ''a'' within the
enclosing function; again, the heuristic is similar, depending only on
whether the compiler had seen another assignment to a before it saw the
block (Python forbids the rebinding of variables coming from an
enclosing but non-global scope, to avoid facing this issue).

All in all, Python is more likely with these heuristics to respect the
principles (from the "zen of python", import this at an interactive
prompt): in face of ambiguity, refuse the temptation to guess; errors
should not pass silently, unless explicitly silenced. I.e., any Python
code which accidentally runs afoul of these heuristics is likely to
raise an exception, alerting you to the situation. Ruby strives rather
for a "do what I mean" ethos, and obviously some people prefer that.

I also share your preference for a single namespace for callable and
non-callable values, as in Python (and Scheme, Lisp, C++, ...), rather
than disjoint namespaces as in Ruby (and Smalltalk), but I do not see it
as a question of rigor and consistency at all -- e.g., I do not perceive
Smalltalk as less rigorous or consistent than C++, on the contrary.

So, I agree with your choice, and I think I understand your motivations,
but I do not entirely share your motivations, personally speaking.
Alex


这篇关于为什么我选择Python而不是Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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