RubyからPythonを学ぶ; 相違点と類似点 質問する

RubyからPythonを学ぶ; 相違点と類似点 質問する

私は Ruby をよく知っています。現時点では Python を学ぶ必要があると思います。両方を知っている人にとって、この 2 つの概念の類似点と相違点は何でしょうか?

私が書いた入門書に似たリストを探していますJavaScript ユーザーのための Lua 学習: 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.

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.

ベストアンサー1

Here are some key differences to me:

  1. Ruby has blocks; Python does not.

  2. 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.

  3. 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.

  4. 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]
    

    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}
    

    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 also supplies similar map comprehensions.

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

  6. Ruby supports switch/case statements; Python does not.

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

  8. 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.

  9. Pythonは1行のラムダ関数のみをサポートしています。Rubyのブロックはラムダ関数の一種であり、任意の大きさにすることができます。このため、Rubyのコードは通常、Pythonのコードよりも関数的なスタイルで書かれています。たとえば、Rubyでリストをループするには、通常次のようにします。

    collection.each do |value|
      ...
    end
    

    ブロックは、関数が渡されるのと非常によく似た働きをしますcollection.each。Python で同じことを行う場合は、名前付き内部関数を定義し、それをコレクションの各メソッドに渡す必要があります (リストがこのメソッドをサポートしている場合)。

    def some_operation(value):
      ...
    
    collection.each(some_operation)
    

    これはあまりうまく流れません。そのため、Python では通常、次の非関数型アプローチが使用されます。

    for value in collection:
      ...
    
  10. リソースを安全に使用する方法は、2 つの言語ではまったく異なります。ここでの問題は、何らかのリソースを割り当て (ファイルを開く、データベース カーソルを取得するなど)、それに対して任意の操作を実行し、例外が発生した場合でも安全な方法でリソースを閉じることです。

    Ruby では、ブロックの使用が非常に簡単なので (9 を参照)、通常、このパターンは、リソースに対して任意の操作を実行するためのブロックを受け取るメソッドとしてコーディングします。

    Pythonでは、任意のアクションに関数を渡すのは、名前付きの内部関数を記述する必要があるため、少し面倒です(9を参照)。代わりに、Pythonはwith安全なリソース処理のためにステートメントを使用します。Python オブジェクトを正しくクリーンアップするにはどうすればよいですか?詳細については。

おすすめ記事