设为首页 收藏本站
查看: 384|回复: 0

[经验分享] Compare Ruby and Python [摘抄]

[复制链接]

尚未签到

发表于 2017-4-24 11:22:35 | 显示全部楼层 |阅读模式
  原文地址:http://jesusphreak.infogami.com/blog/why_py
  Ruby's warts


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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-368589-1-1.html 上篇帖子: Python Strings笔记 下篇帖子: python文件的编码问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表