`
charka
  • 浏览: 3545 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Compare Ruby and Python [摘抄]

阅读更多

原文地址: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.

分享到:
评论

相关推荐

    Ruby-JsonCompare返回两个JSON文件之间的差异

    本篇文章将深入探讨`JsonCompare`的功能、使用方法以及如何在Ruby环境中应用它。 `JsonCompare`是一个实用的工具,它的主要目标是快速准确地找出两个JSON文件之间的差异。这对于测试、数据分析或者验证API响应非常...

    Python Unit Test Automation:for Python Developers and Testers

    All of which makes this book a great resource for software developers and testers who want to get started with unit test automation in Python 3 and compare the differences with Python 2. This short ...

    Python库 | compare_package_ex3-0.0.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:compare_package_ex3-0.0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    matlab_ 红黑树二分搜索法示例,用于比较C++、Java、Python、Ruby和MATLAB代码

    C++、Java、Python、Ruby和MATLAB OOP实例比较 红黑树二分搜索法示例,用于比较C++、Java、Python、Ruby和MATLAB...RedBlack Tree Binary Search Example Used to Compare of C++, Java, Python, Ruby and MATLAB Code

    Python库 | human-name-compare-0.2.2.tar.gz

    《Python库human-name-compare-0.2.2:实现人名比较的高效工具》 在信息技术领域,Python作为一种简洁且强大的编程语言,拥有丰富的库支持,使得开发工作变得更加便捷。今天我们要关注的是一个名为"human-name-...

    Linqer compare SQL and LINQ files

    Linqer is a software application which can be used in order to compare SQL and LINQ files, as well as run them, and convert the first type of database to the other. This tool is portable, which means...

    python2.7 face compare

    需要的支持环境 python2.7.13 numpy-1.11.3+mkl-cp27-cp27m-win32.whl opencv_python-3.1.0-cp27-cp27m-win32.whl 在windows10环境下测试通过 可以完成人脸图像的的识别和比对。

    CloudCompare的源代码

    CloudCompare是一款开源的3D点云处理软件,它提供了丰富的功能,包括点云的导入、显示、编辑、分析以及导出等。源代码的开放使得用户可以深入理解其内部工作原理,同时也为开发者提供了二次开发的可能性。对于学习...

    Mastering Python Data Visualization(PACKT,2015)

    and explore some interesting data structures that come with it Use various visualization techniques to explore how Python can be very useful for financial and statistical computations Compare Python ...

    compare_excel_excelpython_pythonexcel_python_excel_

    在IT行业中,Python是一种强大的编程语言,尤其在数据处理和自动化任务方面表现出色。本项目主要探讨如何利用Python来比较两个文件夹中的Excel文件内容,并记录下它们的差异。这在数据分析、审计或质量控制等场景中...

    BeyondCompare4 and photoshop

    标题 "BeyondCompare4 and Photoshop" 暗示了本次讨论的主题是关于两款强大的软件:Beyond Compare 4 和 Adobe Photoshop。这两款工具虽然都是在IT领域内广泛应用,但它们的功能侧重点不同。Beyond Compare 4 是一款...

    Website Scraping with Python: Using BeautifulSoup and Scrapy

    You’ll review which tools to use, and compare their features and efficiency. Focusing on BeautifulSoup4 and Scrapy, this concise, focused book highlights common problems and suggests solutions that ...

    3D-RCNN: Instance-level 3D Object Reconstruction via Render-and-Compare

    Render-and-Compare loss that allows 3D shape and pose to be learned with 2D supervision. We evaluate our method on the challenging real-world datasets of Pascal3D+ and KITTI, where we achieve state-of...

    Compare Chinese and Western Privacy through Social Media Privacy

    Compare Chinese and Western Privacy through Social Media Privacy Policies212Shub

    python compare DB

    本项目"python compare DB"旨在利用Python进行数据库记录的比对,并将结果导出为Excel文件,适用于Oracle和MySQL等常见数据库系统。以下是关于这个项目的一些关键知识点和实现细节: 1. **Python版本**:项目要求...

    点云处理软件cloudcompare源代码

    8. **脚本与自动化**:支持通过Python脚本控制软件,实现批处理和自动化流程。 对于学习点云处理程序开发的同学,研究CloudCompare源代码可以收获以下知识: 1. **数据结构与算法**:了解如何高效存储和处理大量三...

    IDM.UltraCompare.Professional.v6.30.Incl.Keymaker-ZWT

    A complement to your file management suite, UltraCompare Professional is loaded with features to enable you to compare text files and folders, as well as zip files and jar archives. Text file compare ...

    Machine Learning for OpenCV: Intelligent image processing with Python

    Evaluate, compare, and choose the right algorithm for any task Who This Book Is For This book targets Python programmers who are already familiar with OpenCV; this book will give you the tools and ...

Global site tag (gtag.js) - Google Analytics