Take the 2-minute tour ×

My original question was too vague and was "closed as not constructive." As such, I will revise the question so that it caters to the answers that have already been posted. :-)

I am interested in the differences between Perl's Moose OO framework Moose and Python's stock OO framework. As a first point of comparison, how easy is it to create a simple class with a couple of attributes and a handful of methods?

Now, if this question gets reopened, I would also like to know: How easy is it to refactor code in the future if I decide to make an attribute "read only"? In other words, what steps would I have to take to change an attribute from being readable and writable to being read only? (I know, it's bad to change an API, but let's assume I'm working on something internally and realize in the middle of my implementation that an attribute really should be read-only.)

share|improve this question

closed as not constructive by unutbu, Wooble, Josh Caswell, ChrisF, BoltClock Feb 8 '12 at 10:03


This is hilarious considering that Perl took much of its original object system from Python. Calling Python's great is a very curious view. –  tsee Feb 8 '12 at 18:10
This comment doesn't make any sense in light of my latest edit. Originally, I said that I had heard that Python's OO is great. What I meant by that is "Python provides syntactic sugar so that creating a class is really easy." –  David Mertens Feb 9 '12 at 15:55
@David Mertens: Regarding your edit - To change an attribute from read/write to read only is easy. For eg. change has someattr => (is=>'rw'); to has someattr => (is=>'ro'); After this change all attempts to write to attribute will give a run-time error, for eg. $x->someattr("foo"); (setter) would throw an error whereas the $x->someattr (getter) is fine. –  draegtun Feb 9 '12 at 16:43
Well, the comment did make perfect sense (to me) when I made it :) –  tsee Feb 11 '12 at 10:38
add comment

2 Answers

Python's OO strength is probably in its pervasiveness. That is, the built-in OO system is so simple and the syntax is so approachable that it's far and away the dominant Python programming paradigm. For instance, the rough equivalent of Todd Gardner's first "Person" example might work like:

class Person:
    def __init__(self):
        self.firstname = None
        self.lastname = None

me = Person()
me.firstname = 'Kirk'  # These two lines update instance attributes
me.lastname = 'Strauser'

This version defines getters and setters for accessing the values:

class AnotherPerson:
    def __init__(self):
        self._firstname = None
        self._lastname = None

    def firstname(self):
        return self._firstname

    def firstname(self, newname):
        self._firstname = newname

    def lastname(self):
        return self._lastname

    def lastname(self, newname):
        self._lastname = newname

you = AnotherPerson()
you.firstname = 'David'  # These two lines call instance methods
you.lastname = 'Mertens'

Python and Perl are roughly equivalent in power, flexibility, and expressiveness. If you can do something in one, you can probably do it similarly in the other. However, I think Python has the clear advantage in simple OO design - to the point that there's not been a reason for any alternative object models to gain significant traction.

share|improve this answer
thanks. Combined with Tood Gardner's response, we have a simple side-by-side comparison and Moose and Python OO look about the same. –  David Mertens Feb 8 '12 at 4:48
In Moose, you get basic getters and setters for free: my $me=Person->new;$me->first_name("Jack");$me->last_name("Maney"); You can even set attributes as read-only right out of the box (and an error is thrown if you try to change the value of a read-only attribute), eg has 'ssn'=>(isa=>'Str',is=>'ro'); Also, you can even specify custom getter and setter names via the reader and writer options. –  Jack Maney Feb 8 '12 at 4:49
While it doesn't appeared to have gained significant traction there is a port which provides a subset of Moose to Python called Bullwinkle: pypi.python.org/pypi/bullwinkle –  draegtun Feb 8 '12 at 17:54
re: Jack's comment - Here is the AnotherPerson class in Moose. gist.github.com/1782691 –  draegtun Feb 9 '12 at 20:08
add comment

From having used both, Moose's big strength is it's brevity. Compare classic perl OO:

package Person;
use strict;
use warnings;

sub new {
  my $self = {};
  $self->{FIRST_NAME} = undef;
  $self->{LAST_NAME} = undef;
  bless ($self, $class);
  return $self;

sub first_name {
  my $self = shift;
  if (@_) { $self->{FIRST_NAME} = shift }
  return $self->{FIRST_NAME};

sub last_name {
  my $self = shift;
  if (@_) { $self->{LAST_NAME} = shift }
  return $self->{LAST_NAME};


with Moose:

package Person;
use Moose;
use namespace::autoclean;



And I'm pretty much sold, but Moose is just getting start. I think my next favorite feature was the Types, which could really simplify a program over a large codebase and stop a whole range of nasty bugs. For instance, it would have nicely handled one I got bitten by the other day (while coding Python, actually) where a certain property of an object was a Date in some instances, but a string representing a date in others.

Haven't heard of any alternative OO system for python.

share|improve this answer
@jdi nonsense, especially given that Perl's and Python's basic OO models are the same, and Python's property for instance was an afterthought added late in the game, Python used to have DFS MRO and later retrofitted C3, etc. –  hobbs Feb 8 '12 at 3:12
@jdi - I actually wrote something like that at first, but I erased it because comparing the two, IMO, Moose is pretty far ahead of stock python OO in things like having typed properties and enums (but that might just be my bias towards static typing.) –  Todd Gardner Feb 8 '12 at 3:24
@jdi - "OO for Perl was more of an afterthought." You have repeated what many have said in ignorance. If you have nothing intelligent to say about Perl's OO then I would appreciate if you would stay on the sidelines. –  David Mertens Feb 8 '12 at 3:36
David, if my text was repeated it was due to user input error and not ignorance. No need to be rude. I also did not suggest Moose or perl were inferior. I simply auggested that an alternate OO system for python seems unnessesary. –  jdi Feb 8 '12 at 3:40
Yes David I agree you can write perl OO without extensions or wrappers and yes I have little experience with perl. My comment was based on the general consensus I have interpreted that it had been a mess until moose. And yet again, my only real statement here was regarding python not needing alternate OO solutions. Lets not nitpick and start a perl python flame war –  jdi Feb 8 '12 at 4:02
show 7 more comments
