TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Python for Ruby Programmers (LA RubyConf 2013)

62 点作者 facebiff大约 12 年前

13 条评论

masklinn大约 12 年前
To add to other comments, things the author could have expanded upon:<p>* vararg keyword arguments (<i></i>kwargs)<p>* Keyword-only arguments (Python 3)<p>* help, vars, a few other nice introspective builtins<p>* Assignment unpacking (multiple names as left-hand side of an assignment)<p>* The strangest "first blush" difference between Ruby and Python, or at least the one which I found oddest when I first started switching, is that a new Ruby class is defined from the `class {Name}` line, the whole body executes in the context of the already-created class. Not so in Python, the class object will only exist once the body has finished executing.<p>* No mention of generators? Also outer v inner iteration is a pretty big split between two otherwise similar languages.<p>Mistake-ish<p>* obj.__dict__ -&#62; vars(obj)<p>* obj.__class__ -&#62; type(obj)<p>* Python's lambdas are not one-line, they're one-expression. The reason you "can't do [this] in Python" is that if:elif:else: is a statement, so doesn't fit in lambdas. But you can have arbitrary complex code in lambdas aside from that, by following <a href="http://codon.com/programming-with-nothing" rel="nofollow">http://codon.com/programming-with-nothing</a>. For instance you can (although you should not) write:<p><pre><code> map(lambda num: 0 if num % 3 == 0 else num * 2 if num % 4 == 0 else num, range(1, 6)) </code></pre> * Author talks about methods a lot. They're not methods, a method is part of an object. They're functions. Granular functions, inner functions, ...<p>* We generally don't say Python values "evaluate to False", we say they're falsy: they're <i>never</i> equal to False (outside of False and — for historical reasons — 0), they're just equivalent when evaluated in a boolean context (or passed to `bool()`). Also, the list is <i>way</i> incomplete: <i>any Python type defines its own "falsiness"</i> by implementing __bool__ (or __nonzero__ in Python 2)<p>* Python's reflection and metaprogramming are roughly on par with Ruby's, their lack of use is more of a cultural artefact (possibly historically motivated) than a technical one. Consider adding methods to existing types for instance. You can do it in Python (on python-defined types), you just very rarely do, it's not in the culture to do something like that. Same with using metaclasses, it's done but it generally isn't the first thing a Python developer will reach for, aside from things like registering classes on the fly/on definition<p>Odd comments:<p>* Ruby mixins, as far as I know, work by injecting the module in the inheritance chain somewhere above the new class. MI is clearer and just as powerful.
评论 #5295129 未加载
评论 #5295980 未加载
评论 #5296359 未加载
arxanas大约 12 年前
The presentation incorrectly identified Python dictionaries as “lists”. It also identified actual Python lists as lists.<p>The presentation states that tuples are “immutable lists“ and that they ”should be homogeneous, but [this is] not enforced“. I disagree: tuples are meant to act as “records“ (as in a database or other set of data), which are neither lists nor homogeneous.<p>The presentation brought up multiple times filter and map. An article by Guido in 2005 [1] argued that these tools should be cut from Python 3. While they still exist, I am under the impression it is considered more Pythonic to use list comprehensions in the place of filter and map, as stated in the article.<p>Python not having define_method is misleading. One can define a method of a class as one would any attribute of the class [2]. However, it is far easier to dynamically define a method in Ruby than in Python, because lexical scoping is inherent to a Ruby block but not a Python loop.<p>Python not having method_missing is wrong. One can simply override __getattr__ and return the appropriate function [3].<p>[1]: <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=98196" rel="nofollow">http://www.artima.com/weblogs/viewpost.jsp?thread=98196</a><p>[2]: <a href="http://ideone.com/njXDzO" rel="nofollow">http://ideone.com/njXDzO</a><p>[3]: <a href="http://ideone.com/EB9MQ8" rel="nofollow">http://ideone.com/EB9MQ8</a>
评论 #5294874 未加载
评论 #5294910 未加载
评论 #5295721 未加载
ubernostrum大约 12 年前
Not bad, but the big weakness is that the author seems to be very familiar with more expert-level aspects of Ruby, but not so much with Python. And turns that into "Python doesn't have these".<p>For example, he mentions Python doesn't have an equivalent of method_missing -- it's technically true that there's nothing you define on a Python class to <i>specifically</i> intercept a nonexistent method, but that's because Python's standard facility for this operates more generally at the level of attribute access. Suspect there's a bit too much expectation of Python to be message-passing like Ruby in not seeing that one.<p>Similarly, Python has plenty of metaprogramming features, they're just implemented differently -- and from a different conceptual standpoint -- than Ruby's. And so on and so forth.
评论 #5294731 未加载
facebiff大约 12 年前
Author here: Thanks much for the feedback. I definitely have more experience in Ruby than Python. In a longer version of this talk, I go into more detail on list comprehensions, iterators and decorators. I'll work to include these to some degree for the shorter talk too.<p>Also, if I say "Python doesn't have these," I'm often saying it in support of Python! :)
评论 #5295158 未加载
danjaouen大约 12 年前
Technically, you can open up Python classes:<p><pre><code> class Test(object): pass t = Test() def test(self): print('ehlo') Test.test = test t.test() </code></pre> This is rarely done in practice, however (at least, as far as I can tell)
评论 #5295173 未加载
lardissone大约 12 年前
Weird that author never mentioned PEP8 guide. I think it's the main thing that difference Python from Ruby developers. If you follow the PEP8 rules, you can interact with other Python programmers without much problems, and enforce you to be a better and organized programmer.
评论 #5294849 未加载
rraval大约 12 年前
I know very little of Ruby, but difference 13 states that Ruby has stronger metaprogramming features in that "Python doesn't have define_method, class_eval, and method_missing". Are these really things that can't be implemented with metaclasses and overloading __getattr__?
评论 #5294918 未加载
ultimoo大约 12 年前
Thanks for posting. As a Ruby programmer I have been wanting to learn Python since 2 weeks and hopefully this will get me started for good.
评论 #5294826 未加载
ufo大约 12 年前
Broke my heart when he said you shouldn't need powerful anonymous functions :(
评论 #5294938 未加载
tragomaskhalos大约 12 年前
As a longtime Ruby person who has recently been doing a bit of Python, I find Ruby to be more regular (echoing its Smalltalk heritage, another plus point), but the one Python thing that really jarred - but which I have never seen anyone else complain about! - was having to put all those pesky colons in after defs, ifs etc.<p>On the credit side, I don't understand why anyone would grumble about the indentation thing - this comes very naturally, and has the pleasing side-effect of gently coercing you into writing shorter functions.
评论 #5297685 未加载
dstywho大约 12 年前
One of the things that bothers me about python is the way you have to specify 'self' for instance methods. Instance methods should be the norm not the exception.
评论 #5295218 未加载
评论 #5294907 未加载
评论 #5295392 未加载
pwim大约 12 年前
One point about "Functions as variables". Ruby actually does have method objects, which you can access via the method method.<p><pre><code> def add(a,b) a+b end def process_numbers(a,b,method) func.call(a,b) end method(:add) =&#62; #&#60;Method: Object#add&#62; process_numbers(1,2,method(:add)) =&#62; 3 </code></pre> This isn't a normal programming paradigm in Ruby though.
MrBra大约 12 年前
"elif" just doesn't sound good :P