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

Duncan Booth me at privacy.net
Thu Feb 26 04:34:11 EST 2004


Joe Mason <joe at notcharles.ca> wrote in 
news:slrnc3q0j8.jm5.joe at gate.notcharles.ca:

> A better example of buond vs. unbound methods is this:
> 
> def boundFunc(callback):
>     print callback(5, 6)
> 
> def unboundFunc(obj, callback):
>     print callback(obj, 5, 6)
>     
> def functionCallback(a, b):
>     return a + b
> 
> class Foo:
>     def methodCallback(self, a, b):
>         return a * b + self.c
>     def setc(self, c):
>         self.c = c
> 
>>>> boundFunc(functionCallback)
> 11
>>>> f = Foo()
>>>> f.setc(3)
>>>> boundFunc(f.methodCallback)
> 33
>>>> unboundFunc(f, Foo.methodCallback)
> 33

Say I modify your example so that we only have one Func which accepts 
either a bound or unbound (functionCallback and Foo definitions are 
unchanged):

>>> def Func(callback, *extra_args):
        print callback(*(extra_args+(5,6)))

        
>>> Func(functionCallback)
11
>>> f = Foo()
>>> f.setc(3)
>>> Func(f.methodCallback)
33
>>> Func(Foo.methodCallback, f)
33
>>> 

Python doesn't care what type of callable it is passed, so long as it 
somehow ends up with the right arguments to call it. Can Ruby handle this 
case?



More information about the Python-list mailing list