Just to complete the Martijn Pieters answer , which was probably your answer, I can't help but show you some very interesting things that you can do in Python, and that I was very surprised when I found them out. I thought you might find them useful or give you some ideas for the future.
1. Python list is an object, so you can extend your class from list
This will make your Bundle object inherit all methods from the built-in list object and allow you to add new methods to it.
#!/usr/bin/env python # -*- coding: utf-8 -*- class Unit(object): def __init__(self): self.val = 0 def __repr__(self): return "%s %s val=%s" % (type(self), hex(id(self)), self.val) class Bundle(list): def __init__(self, N=3): super(Bundle, self).__init__() self.extend([ Unit() for i in range(N) ]) @property def vals(self): return [ unit.val for unit in self] @vals.setter def vals(self, vals): vals = vals[0:min(len(self), len(vals))] # if len(vals) > len(self), self[i] would break for i, val in enumerate(vals): self[i].val = val if __name__ == "__main__": bundle = Bundle() print "Bundle: %s" % bundle newUnit = Unit() bundle.append(newUnit) print "Bundle: %s" % bundle bundle.vals = [1, 2, 3, 4, 5, 6] print "Bundle (reassigned): %s" % bundle
Note that I changed the definition of the property a bit to make them decorators, but the basic idea remains the same.
2. You can overwrite certain built-in methods and have an object that is not list , like list :
Keep in mind that this code is for trial purposes only. It has a terrible design, horrible use of OOP, its behavior would be very confusing for everyone (even for Guido van Rossum ... well, maybe not for him, but I'm sure he cried if he saw this in a real program ) This is part ...... of bad code, but I think it can help to understand that you can rewrite inline methods. In addition, he lacks many methods for rewriting for the Bundle class, which behaves like a real list object, but I got a little tired :-). Check out Container Type Emulation and the next paragraph, Additional Python Documentation Sequence Type Emulation Methods, for full reference.
#!/usr/bin/env python # -*- coding: utf-8 -*- class Unit(object): def __init__(self): self.val = 0 def __repr__(self): return "%s %s val=%s" % (type(self), hex(id(self)), self.val) class OtherUnit(object): def __init__(self): self.whatever = "hello" def __repr__(self): return "%s %s whatever=%s" % (type(self), hex(id(self)), self.whatever) class Bundle(object): def __init__(self, N=3): self.units = [ Unit() for i in range(N) ] self.otherUnits = [ OtherUnit() for i in range(N) ] def __repr__(self): return "%s" % (self.units + self.otherUnits) def __len__(self): return len(self.units) + len(self.otherUnits) def __iter__(self): for item in (self.units + self.otherUnits): yield item def __contains__(self, value): if isinstance(value, Unit): return value in self.units elif isinstance(value, OtherUnit): return value in self.otherUnits elif isinstance(value, int): return value in [unit.val for unit in self.units] elif isinstance(value, str): return value in [otherUnit.whatever for otherUnit in self.otherUnits] else: return False def __getitem__(self, index): assert index >= 0, "Can't accept negative indexes (%s)" % indexes if index < len(self.units): return self.units[index] else: return self.otherUnits[index - len(self.units)] #Will raise index error if too big def append(self, thing): if isinstance(thing, Unit): self.units.append(thing) elif isinstance(thing, OtherUnit): self.otherUnits.append(thing) else: raise TypeError("Can't accept %s" % type(thing)) @property def vals(self): return [ unit.val for unit in self.units] + [ otherUnit.whatever for otherUnit in self.otherUnits ] @vals.setter def vals(self, vals): insertionPointUnits = 0 insertionPointOtherUnits = 0 for i, val in enumerate(vals): if isinstance(val, int): self.units[insertionPointUnits].val = val insertionPointUnits += 1 elif isinstance(val, str): self.otherUnits[insertionPointOtherUnits].whatever = val insertionPointOtherUnits += 1 if __name__ == "__main__": bundle = Bundle() print "Bundle: %s" % bundle newUnit = Unit() bundle.append(newUnit) print "Bundle: %s" % bundle bundle.vals = [1, 2, "bye", 3, "how are you", 4, "doing ok"] print "Bundle (reassigned): %s" % bundle print "Bundle has %s items" % len(bundle) #Thanks to overwritting __len__ for i, item in enumerate(bundle): print "bundle[%s]: %s" % (i, item) #Thanks to overwritting __iter__ print "Does 'bundle' contain 'bye'?: %s" % ('bye'in bundle) #Thanks to overwritting __contains__ print "Does 'bundle' contain 5?: %s" % (5 in bundle) #Thanks to overwritting __contains__ print "Item 1 (should be Unit with val '2': %s" % bundle[1] #Thanks to overwritting __getitem__ print "Item 5 (should be OtherUnit with val 'how are you' (4 Units + 1 OtherUnit... then ours!): %s" % bundle[5] #Thanks to overwritting __getitem__ try: print "Item 9 (should raise IndexError): %s" % bundle[9] except IndexError, ie: print "Wooops: %s" % ie
Hope this helps a bit. Have fun with Python!