How does Ruby compare to Python?? How good is DESIGN of Rubycompared to Python?

Dave Brueck dave at pythonapocrypha.com
Wed Feb 25 08:57:21 EST 2004


Joe wrote:
> > * In Python, functions are first-class, and
> > methods are implemented in terms of functions. In Ruby,
> > methods are the fundamental concept, and there are no
> > first-class functions. The result is that Python lets
> > you obtain a bound method from an object and use it like
> > any other function. You can't do that in Ruby. You can
> > get a method object in Ruby, but you can't call it using
> > normal calling syntax.
>
> I don't see the distinction.  "normal calling syntax" in ruby involves
> an object, so "unbound function" isn't a meaningful concept.  I mean, if
> you get a method the begins with the self parameter, you still need an
> object to call it, right?

No - that's the difference between a bound and unbound method (see below).

> Even if you're calling it as "foo(obj,
> params)" instead of "obj.foo(params)".  I don't see what the ability to
> use the other syntax gets you, except the ability to pass functions
> around independantly of objects, which I'm pretty sure you can do with
> methods in Ruby anyway.

As for whether or not Ruby supports this, I'm in the don't-know-don't-care
camp, but to clarify: a bound method "knows" which object instance it belongs
to. Given:

def someFunc(callback):
  print callback(5,6)

def functionCallback(a, b):
  return a + b

class Foo:
  def methodCallback(self, a, b):
    return a * b

then both these work:

someFunc(functionCallback)
f = Foo()
someFunc(f.methodCallback)

This is pretty darn useful and IMO quite Pythonic: the creator of the function
and the creator of the callback have to agree on only the most minimal set of
details - just those relating to the calling interface - leaving completely
open any implementation details.

-Dave





More information about the Python-list mailing list