Python questions from C/Perl/Java programmer

Tim Peters tim_one at email.msn.com
Sat Sep 9 04:53:09 CEST 2000


[Grant Griffin, on OO in Perl5]
> ...
> I guess I think of it more as a well-thought-out, very clever kludge.
> (BTW, Tim, do you think Larry blessed the addition of the "bless"
> keyword or did they slip that one past him? <wink>)

I was still (although less than half-heartedly) reading c.l.p.m when
OO-in-Perl was being dreamt up, and "bless" was definitely introduced in a
msg from Larry.  IIRC, that msg was met with silence -- I'm not sure anyone
understood what he was talking about.  For that matter, I'm still not
<wink>.

> Perl's OO seems like a clever (if cumbersome) way to extend the
> capabilities of Perl 4 for purposes of OO <<still a kludge>>.

Very clever, yes, but very raw too.  Once you get over the magic of blessing
a pointer into a package, though, the rest of it is actually very much like
Python!  Or like Python would be if you had to manipulate the class
namespace and the instance __dict__s explicitly all the time.  You have to
squint a bit to see Perl package lookups as Python dict lookups, but you
don't have to squint hard.  I never  really figured how to do OO in Perl5
from the docs; the "aha!" didn't come until I realized that that it was
mostly a low-level reimplementation of Python's internals, with the
additional gimmick that an instance's concrete representation could be any
kind of reference (not just to a dict/hash -- although I'm not sure I've
ever seen anything other than a dict ref get blessed in real life).

> ...
> But now that I've learned Python's approach to OO <<falling off
> a log>>, maybe Perl's will make more sense <<or not>>.

In Python, an instance gets hooked back to its class via its __class__
attribute.  In Perl, an instance is a reference that got bless'ed into a
package, and the package does double-duty as a class, and the mechanics of
blessing are hidden from you.  But it does no real harm to think of blessing
a reference as installing a __class__ attribute!  Same effect in the end.

After that hurdle, the rest is pretty straightforward.  Python supplies an
instance __dict__ for you, but hides it under nice syntax for all "normal"
uses.  Perl5 makes you create the instance hash yourself, and shoves it in
your face all the time via sticking to "dict syntax" for lookups.  It's
actually *that* minor part that drove me insane.  The difference between

    obj.attr

and

    $ojb->{attr}

repeated 10000x over was just unbearable.  Curiously, Perl folk are *very*
sensitive to minimizing keystrokes when it comes to regexps <wink>.

although-not-when-it-comes-to-curly-braces-go-figure-ly y'rs  - tim






More information about the Python-list mailing list