翻译,python和ruby的区别和特点

原文如下:

http://www.ruby-lang.org/zh_CN/documentation/ruby-from-other-languages/to-ruby-from-python/

Python is another very nice general purpose programming language. Going from Python to Ruby, you’ll find that there’s a little bit more syntax to learn than with Python.

Similarities

As with Python, in 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).

Differences

Unlike Python, in 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 re-open a class anytime and add more methods.
  • 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

 

翻译转自博客园 http://www.cnblogs.com/gpcuster/archive/2008/11/24/1339679.html

Python是一门非常优秀的语言,从Python迁移到Ruby,你将发现一些语法上的不同。

 

相似点

 

Python一样,在Ruby,...

  • 有一个交互提示 (叫做 irb).
  • 你可以在命令行中读取文档 (通过ri 命令来替代 pydoc).
  • 没有特殊的结束一行的符号 (新行除外).
  • 文字可以用多行,就像Python中的三个引号.
  • List[]Dict{} (DictRuby中叫“hashes”).
  • Arrays的工作方式相同(2Array相加成为一个更加长的Array,但是想这样a3 = [ a1, a2 ] 合并,将产生一个包含数组的数组).
  • Objects是有固定类型和动态转换的.
  • 一切都是object,变量名只是一个指向Object的指针.
  • 虽然关键字不同, 但是exceptions的工作方式不变.
  • 你拥有嵌入文档的工具 (Ruby中叫做rdoc).

 

不同点

 

Python不同, Ruby,...

  • Strings是可以改变的。
  • 你可以使用常量(常量的值是不同改变的)
  • 这里有些强制规定的书写要求 (例如:class的名字以大写字母开头, 变量名已小写字母开头)
  • 这里只有一种容器(Array), 并且是可以改变的。
  • 引号中的String限制不同。
  • 这里没有新风格的ClassClass只有一种风格。
  • 你无法直接访问属性,在Ruby中,都是通过方法调用实现的。
  • 在方法调用中使用(),是一种可选的策略。
  • 这里用private等等限制访问的关键字,来替代Python中的名字隐藏。
  • “mixin’s”用来替代多继承。
  • 你可以在任何时候修改已有的Class,并添加新的方法。
  • truefalse来替代TrueFalse (nil来替代None)
  • 在判断真值的时候,只有falsenil会被认为是假.其他所有的都认为是真(包括0, 0.0, "",[])
  • elsif替代elif.
  • require替代import. 但是用法是相同的。
  • usual-style来注释文档(替代docstrings) 同时用来生成文档。
Tags:  python教程 python平台 python python教程 python平台 python

延伸阅读

最新评论

发表评论