a Python person's experience with Ruby

Steve Howell showell30 at yahoo.com
Sun Dec 9 15:38:37 EST 2007


--- MonkeeSage <MonkeeSage at gmail.com> wrote:
> 
> Not just callable, but interchangeable. My point was
> that in ruby, if
> you use a block or a lambda as a HOF, you have to
> use #call / #[] /
> yield keyword on it to call it.
> 
> def foo(a)
>   puts a
> end
> bar = lambda { | a | puts a }
> 
> # these do the same thing
> [1,2,3].each(&bar)
> [1,2,3].each(&method(:foo))
> 
> That's not to say it's better than python (like I
> said, I personally I
> like pythons referencing / calling convention a
> little better), it's
> just that since Proc objects already have that call
> syntax in ruby,
> making method references use it also allows them to
> be interchanged (w/
> o having to do method(:foo).to_proc).
> 

Jordan and others, thanks for all your posts; I am
learning a lot about both languages.

This is what I've gathered so far.

Python philosophy:
   passing around references to methods should be
natural (i.e. my_binary_op = math.add)
   calling methods should be explicit (use parens)
   the use of setters/getters varies among Python
programmers; properties, decorators, special methods,
etc. can be used judiciously to affect the interface
   
Ruby philosophy:
   a method itself should be callable without parens
   you can get a reference to a chunk of code, but
then you need a little extra syntax, beyond just a
variable name and parens, to eventually call it
(yield, &, call, etc.)
   when referring to methods, you can use :symbols to
name the method you're interested in without actually
calling it

My personal experience:

   Even after doing lots of Python, I occasionally got
bitten by the pitfall of omitting the parens when I
meant to call something, but it was never major pain.
(I never made the opposite mistake, in case you're
wondering.)

   Despite the pitfall above, I always liked the
tradeoff that Python gave me more natural syntax for
passing around methods.  (And, more fundamentally, I
like the Python notion of binding "general" things to
a  name.)

   As somebody just starting to use Ruby, I actually
like omitting parens in method calls, which I view as
the more common case.  I admit some cost here, though,
in simple churn of the code due to the fact that some
people like having parens for aesthetic reasons.

   I was surprised in Ruby by how seldom I really pass
references to methods around, but it is definitely
something I want to understand better.

I hope this adds a little perspective, and please feel
free to correct me in cases where I'm either imprecise
or just flat out wrong.



      ____________________________________________________________________________________
Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs



More information about the Python-list mailing list