Need arguments for "Python vs. Perl as an OOPL"

Jeremy Dillworth jwdillworth at yahoo.com
Tue Sep 23 09:17:54 EDT 2003


Actually, Perl does have a "super" construct.

Take this Python script:
class Base1(object):
    def method(self):
        print "Base1.method called"

class Base2(object): 
    def method(self):
        print "Base2.method called"

class Sub(Base1, Base2):
    def method(self):
        print "Sub.method called"
        super(Sub, self).method()

obj = Sub()
obj.method()

Sub.method is called, and it calls Base1.method (but not Base2.method).

This Perl script does exactly the same thing:
package Base1;

sub method {
    print "Base1::method called\n";
}

package Base2;

sub method {
    print "Base2::method called\n";
}

package Sub;
use base qw(Base1 Base2);
sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
}  

sub method {
    my $self = shift;
    print "Sub::method called\n";
    $self->SUPER::method();
}

package main;

my $obj = new Sub();
$obj->method();


Actually, I think I like Perl's super-as-a-property-of-self a tad better than Python's
super-as-a-builtin.  Anyway, if you argue that Perl doesn't have this or that and aren't
sure, you could lose credibility if you're wrong.

Maybe you could find someone in your office well-versed in OO-Perl that's sympathetic. 
Or perhaps you could have a look at Manning's book on OO-Perl by Conway (the PDF is $21).

I don't think Perl's exception handling is very OO (if at all).  Also, errors can very
easily pass silently in Perl if you don't explicitly ask it to make a fuss.

I'd argue that Python's class syntax is simpler and more intuitive than Perl's, and that
should count for something.  If it didn't, everyone would follow the gtk+ folks and be
writing object oriented programs in C :)

(not bashing gtk+ at all, but you have to admit OO-C is unorthodox and a little bit of a
hack)

--- Raymond Hettinger <vze4rx4y at verizon.net> wrote:
> 
> "Roy Smith" <roy at panix.com> wrote in message
> news:roy-002696.23242122092003 at reader2.panix.com...
> > I'm working on a prototype of a new application in Python.  At some
> > point, if this ever turns into a product, the powers that be will almost
> > certainly demand that it be done in Perl.  My job will be to convince
> > them otherwise.
> >
> > The basic design of the application is object oriented.  I've never used
> > Perl's OO features, so I'm not in a good position to make a comparison
> > of the two languages from an OO point of view.  Can somebody who's done
> > OOP in both Python and Perl help me out?
> >
> > I certainly know why Perl sucks in general, but for this purpose, I need
> > to specifically compare the OO features of the two.  I'm looking for
> > something more fundamental than "->{} is ugly".
> 
> If you're to find a major difference in OO implementations, it will likely
> be with Python's new style classes.  So, the advantages are:
> 
> * easy implementation of metaclasses through __metaclass__
> * super() for cooperative superclasses
> * a method resolution order that supports cooperative superclasses
>    and complex inheritance hierarchies
> * __slots__ for lightweight instances
> * o.__getattribute__ for easy intercepts of method calls and attribute lookup
> * descriptors which make easy work of otherwise hard tasks
> * the fruits of descriptors:  class methods, static methods, and properties
>    (which make it possible to avoid coding many getters and setters).
> 
> Other than the new-style gizmos, it's mostly the same candy in a different
> wrapper.
> 
> 
> Raymond Hettinger
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list