tags:

views:

148

answers:

4

I'd like this class to act like a list. It's data resides in the attribute self.data. If I have an instance, pp = population, does defining __getitem__ mean I can refer to pp instead of pp.data? Or is it the defining of __repr__ that does that? Would deriving this class from list instead of object provide me with 'pop'. Right now I need to implement 'pop'method. Thanks

class population (object):
def __init__ (self):
    self.data = []

def append(self, item):
    self.data.append(item)

def extend(self, item):
    self.data.extend(item)

def sort(self):
    self.data.sort(cmp=fitnesscompare)

def __getitem__(self, index): return self.data[index]

def __setitem__(self, index, item): self.data[index] = item

def __len__(self): return len(self.data)

def __repr__(self): return repr(self.data)

def copy(self):
    return copy.deepcopy(self)
+6  A: 

Why not just extend the list class? Then you have all of that functionality built in.

class population(list):
    # custom methods here

Just remember, instead of referencing self.data for the list, just reference self.

Evan Fosmark
Yes, that worked well. I had to add the 'copy' method, thanks
Peter Stewart
+2  A: 
def pop(self, index=-1) :
    return self.data.pop(index)

This will implement the expected behavior :

  • return the last item if no index is passed;
  • return the item at "index" if passed;
  • will use the type check of the underlying pop();
  • will raise the same exceptions as the underlying pop().

I would have sub classed List as Evan Fosmark suggested before, but I can see good reasons for not doing so. Composition can ease low coupling, and you have full control over it. But you have to write bridges for all the methods you want to delegate, it can be a pain...

e-satis
I'll try this. I subclassed population(list). An instance of it has a collection of 'chromosome' class instances which are not themselves iterable. But when I try to 'instOfPopulation.pop()' I get "TypeError: 'chromosome' object is not iterable"
Peter Stewart
I don't really see what went wrong, subclassing or my solution ? And filnally, didn't fosmark solution worked ? In that cas it should be accepted.
e-satis
I still have not been able to use 'pop()' on the population class. When I tried to use your method, the following error message came up:" File "C:\Python26\Code\OO.py", line 201, in pop return self.pop(index)" and scrolled untill "maximum recursion depth exceeded". I'm stumped as to why a list will not 'pop()' it's contents and return an error that the contents themselves are not iterable. I'm new to stackoverflow and didn't realize I should accept an answer. I was able to subclass list as fosmark suggested, but I still havn't been able to implement 'pop()' on population
Peter Stewart
My apologies. I was trying to assign the return from 'pop()' to a list using 'extend()', and it was that which was raising the error. So fosmark's solution worked. Thanks to all who responded.
Peter Stewart
+1  A: 

If I have an instance, pp = population, does defining __getitem__ mean I can refer to pp instead of pp.data?

Essentially, yes. Adding a __getitem__ method is equivalent to overloading the [] operator in some other languages. Thus the following two would be equivalent:

pp.data[0]
pp[0]

Or is it the defining of __repr__ that does that?

Defining __repr__ will give you a string representation of the object. Thus these two calls would be equivalent:

repr(pp.data)
repr(pp)

Would deriving this class from list instead of object provide me with 'pop'.

From what I see right now, inheriting from list is probably the way to go. Unless I'm missing something, the only thing that's different is the sort method and copy. Just about everything else is the same.

Jason Baker
thanks, that clears up a lot
Peter Stewart
+1  A: 

Is this really a list of things, or are you using a list to store a bunch of things? Before you inherit from list, why not just use a list?

I try to save inheritance for things that are true is-a relationships. A Form is-a Window, a Dialog is-a Form, etc. If you are just modeling is-implemented-using-a, then just use the base class, or use containment and delegation.

What is it about a population that is more than just a list?

Paul McGuire