
Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [tlug] ruby and python in Japan
Zev Blut writes:
> Well, this is not restrictive but an annoying aspect is that you must
> explicitly declare self in all of your instance methods. It makes the
> OO aspect of Python feel tacked on.
Ah, you haven't drunk the Kool-Aid. This is an instance of "explicit
is better than implicit." I think you should check your mental model
of OO. I'm not saying that your annoyance is unwarranted---whatever
makes you more productive works for me. My point is that OO and
implicit arguments are independent constructs.
What I do find mendoukusai is that member names don't get special
treatment in members, eg,
class SimpleDate:
def __init__ (self, year, month, day):
self.year = year
self.month = month
self.day = day
OTOH, this idiom is very clear to me as a reader, and the member names
don't need explicitly declaration as in (say) C++. I got used to it.
It would be easy to write an auto-init constructor in XEmacs that
parses the __init_ signature and automagically inserts
"SELFNAME.MEMBERNAME = MEMBERNAME" where SELFNAME is the first
argument, and MEMBERNAME iterates over the rest of the arguments.
> > What's the use-case for changing a class on the fly?
>
> In most cases it is not a good thing, but there are times when it can
> be very useful. For example, if a standard API is broken you can
> simply open the class (such as cgi) and redefine a working method.
> Then, while you are waiting for the fixed patch to be accepted into
> ruby core you can keep using your monkey patch.
Python has been monkey-patching since before Ruby was invented. The
fact that it's not trivial to do is a good thing, IMO.
> Also, Rails does some fun stuff with dates and numbers such as
> "5.days_from_now".
In Python you could spell that
from DateTime import date
five_days_from_now = date.today() + 5
So you're exactly right; it's "fun", but lack of it doesn't hinder
serious work by very much. How much? This much: you'd like to write
from DateTime.date import today
later = today() + 5
but today is overloaded:
from DateTime.datetime import today
later = today() + 5
The Ruby version does have the advantage that by monkey-patching the
Integer class you can put the units in the method name. I'd have to
work with it to decide whether that's a convenience or a trap for me.
N.B. The parens on "today()" is the way that "volatile" is spelled in
Python; it's possible to define computed attributes as long as the
computation requires no arguments.
Home |
Main Index |
Thread Index