欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Compare Ruby and Python [摘抄]

程序员文章站 2022-06-06 23:38:21
...

原文地址:http://jesusphreak.infogami.com/blog/why_py

Ruby's warts

  1. It's slow. Yes, I know that you can rewrite performance-sensitive parts in C, but that should only be a very last resort. With Python, I haven't had these performance issues, and if I really really need to speed them up even more, I can use tools like Pyrex and Psyco before considering breaking that final barrier and using C.
  2. Poor library support. Yes, Ruby is an excellent language. But you can have the most elegant and wonderful language in the world, and if it doesn't have library support, it is ineffective. A lot of apps in Rails are being built without the need of solid-third party libraries, and that's great, but there are those times when you need to read a DBASE file. And there are times when you'd like another templating system than erb. In Python you've got Cheetah, Kid, Myghty, Django's templates, PSP, the list goes on and on. Likewise with ORM systems. In Ruby you essentially have ActiveRecord and Og. Now the number of people using Og probably numbers in the dozens, so you'd be hard-pressed to find adequate support when you need it. In Python you have several projects from SQLObject, Django's ORM, Zope's OODB, or the very useful SQLAlchemy.
  3. Poor language support. Python has a very clear-cut process for users to improve the language. These PEPs (Python Enchancement Proposals) have seen everything from a standard Python style guide (PEP 8), to the implementation of a server interface for all Python web projects, WSGI (PEP 333). With Ruby, where do I turn? Much of the core development discussion is in Japanese, and even if it weren't, where do I formally request improvements to the language? Further on this concept of poor language support, you have Python being used at places like NASA using tools such as NumPy, and Google. Where is Ruby being used heavily internally? When there are NASA engineers using the language and making improvements to tools, I know I'm getting quality. Python is used in so many different fields for so many different uses. Ruby is used mainly for web applications. This means Python has a much wider range of influence and a much greater range of knowledge converging on the language. Its not limited to web programming. And you can see this in the tools put out by the community. Where can I find something similar (and stable) like Pscyo in Ruby? Where can I find the equivalent to NumPy? Pygame?

Ruby and Python compared

So when we get down to it, it's obvious people aren't chosing Ruby over Python for speed or library support. So why are people chosing it? The two biggest reasons I see is because Ruby is a beautiful language, and Rails.

On the topic of Ruby's subjective beauty as a language, Python and Ruby have their differences, but in many ways they are extremely similar.Hell, if Python didn't have its indentation rules, 90% of Python and Ruby code would be identical. An example:

class A:
    def test(self):
        a = "test"
        b = "test 2"
        c = [a, b]
        return c

class A
    def test
        a = "test"
        b = "test 2"
        c = [a, b]
    end
end

They each have their own quirks and there are several things that don't map so cleanly from language to language, but for the most part, these are two very similar languages. It's obvious that over the years they have each borrowed heavily from each other.

Now this is obviously very subjective to each programmer himself, but my main point is, Ruby has better syntax for some things, while Python has better syntax for others. There isn't some massive divide between the two languages where one is a ton cleaner than the other (we aren't comparing Java and Ruby, for example). There are also language features that Ruby is better in, such as blocks (I do love and miss blocks), but there are other areas where Python is a lot better, such as its module support, and Unicode.

Two areas where the two languages do diverge quite drastically is on the topic of metaprogramming and also the idea of explicit over implicit. In Ruby, metaprogramming (programs that write programs) is embraced, while in Python it is seen as something to be avoided for the most part, in favor of code simplicity and readability. This follows the Ruby philosophy of TIMTOWTDI (there is more than one way to do it), versus Python's "there should be one obvious way to do it". Ruby's open-endedness on this is nice in some regards. I enjoyed writing little things like (this is an extremely simple example):

counter += 1 if x == 2

In Python you could also write something much like it (but keep in mind, this is considered a bad thing to do - check PEP 8, under Whitespace in Statements and Expressions):

if x == 2: counter += 1

However, when you get down to it, lots of little shorthand pieces throughout your code will force anyone reading your code to first understand all your own unique coding conventions. It can quickly make reading code more of a process of translating as opposed to just reading.

And that's something I've learned to value in Python. I always had a difficulty reading other people's Ruby code. A lot of times it could be very "hackish", much like people used to complain about Perl. But when I pick up someone else's Python code, because there is one preferred way of doing things, if I understand those community-wide conventions, then I'm going to have no problem reading their code. And this is something that is entirely too underrated.

Web frameworks

And finally, let's get down to what everyone wants to talk about: Rails. As I said before, 6 months ago, Rails really didn't have a lot of competition. Django and TurboGears were gearing up (bad pun, I know), but they weren't at the level of maturity Rails was, and their documentation was rather lacking. This just isn't the case anymore, and not just for these two projects.

Python has something Ruby doesn't, and it's a little something that I mentioned earier, WSGI (Web Server Gateway Interface). WSGI is a standard interface between servers and web applications. If your web framework supports WSGI (and its extremely simple to support), it automatically can run on mod_python, FCGI, SCGI, basically any server that also supports the WSGI standard (and that list is growing). That immediately removes quite a bit of the complexity in making Python frameworks. Some frameworks, such as Pylons, are taking that a step further and integrating WSGI throughout their entire framework stack. This essentially means that any ORM, templating language, session manager, whatever, can be switched out with a few lines of code. Try using something other than ActiveRecord with Rails, and see how easy that is. This is an area where Ruby could learn quite a bit.

Python also has another great little tool built with WSGI, called Paste. Paste essentially makes creating, maintaining, and deploying your app dead-simple. As all of you Rails users know, to create a new Rails app, you simply say:

rails newapp

Using Paste, I can say:

paster newapp --template=pylons

But its longer, and what is that template suffix? With that command we have told Paste to generate a new project, using the template for the Pylons framework. I could just as easily specify the template for a project using TurboGears. I could even go so far as to decide that I love Pylons, and in each Pylons application I make, I always want a specific stylesheet and I want a company login system. No problem, I simply customize a Pylons project and create my own unique template. This is extremely cool. As I said earlier, Paste does many more things than that, but its just another example of a great Python tool that many of these Python frameworks are embracing.

Speaking of tools, I mentioned it earlier, but I must again mention SQLAlchemy. SQLAlchemy is an extremely flexible and powerful Python tool for working with databases. You ever run into legacy databases that simply won't work with ActiveRecord? SQLAlchemy has absolutely no problem with these, because you specify your table metadata using Python code, and then you create a mapper for that metadata. This allows unlimited flexibility. On top of that, if you just want something cut-and-dry simple like ActiveRecord, just use ActiveMapper, a nice little tool built on top of SQLAlchemy. You just don't have these kind of choices in Ruby, certainly not choices that are this heavily used and well-documented.

And this is the great things about all of these Python frameworks; must of them have solid documentation and most of them are being used in production already (much like Rails). Heck, Django powers parts of washingtonpost.com. And if one of these Python frameworks doesn't fit your taste, you aren't forced to accept it or leave the language behind, you have dozens of others to chose from. Want something for content-heavy sites? Use Django. Want something extremely simple? Check out web.py (it runs the very popular Reddit.com, btw). Or hey, maybe you love Rails? You are in in luck there, too - check out Pylons or TurboGears.