Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Moose compare to Python's OO system? [closed]

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.)

like image 476
David Mertens Avatar asked Feb 08 '12 01:02

David Mertens


2 Answers

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 = {};
  my $class = ref($proto) || $proto;
  $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};
}

1;

with Moose:

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

has 'first_name' => (is  => 'rw', isa => 'Str',);
has 'last_name' => (is  => 'rw', isa => 'Str',);

__PACKAGE__->meta->make_immutable;
1;

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.

like image 166
Todd Gardner Avatar answered Sep 21 '22 11:09

Todd Gardner


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

    @property
    def firstname(self):
        return self._firstname

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

    @property
    def lastname(self):
        return self._lastname

    @lastname.setter
    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.

like image 29
Kirk Strauser Avatar answered Sep 20 '22 11:09

Kirk Strauser