[Tutor] Re: Python vs. Ruby
Gregor Lingl
glingl at aon.at
Thu Oct 30 19:20:35 EST 2003
...
>In Ruby, you can subclass everything, modify all classes, call methods on
>everything.
>
>Ruby Python
>=====================================
>2.times do for i in range(2):
> puts "ok" print "ok"
>end
>- - - - - - - - - - - - - - - - - - -
>ok ok
>ok ok
>=> 2
>=====================================
>5.0.+(3) 5.0 + 3
>- - - - - - - - - - - - - - - - - - -
>=> 8.0 8
>
>
at least this last example is not so much different from Python:
>>> 5.0.__add__(3)
8.0
>>>
which shows that in Python + also is only some sort of syntactical
sugar.
On the other hand there are types of objects, e.g. type 'function',
which cannot be subclassed as far as I know (and regret).
Testtask: is it possible to subclass (numerical) functions - say this
new class is named Fun - e.g. by adding a __add__ method, in a
way that their instances can be added, like:
>>> from math import sin, cos
>>> f = Fun(sin) + Fun(cos) ### see remark below
>>> f(1)
#### should output sin(1)+cos(1)
In short: it would be nice to create some sort of function algebra.
In Python this seems a bit weird because of the special
way functions are defined (namely via the reserved word def).
So how should (or could) one define a "fun"? Maybe by making
it "callable" somehow? (Oh, I feel that this is not a very sharply
defined problem ...)
Is a task of this sort more easily ( and/or more
in accordance with the philosophy of the language)
solvable in Ruby?
Gregor
More information about the Tutor
mailing list