Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Frank Millman
On Mar 31, 8:49 am, "Frank Millman" wrote: Hi all Thanks to all for the helpful replies. Rob, you are correct, I had not realised I was adding attributes to the class instead of the instance. Your alternative does work correctly. Thanks. Carl, I understand your concern about modifying attr

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Hrvoje Niksic
"Frank Millman" writes: class MyList(list): > ... def __new__(cls, names, values): > ... for name, value in zip(names, values): > ... setattr(cls, name, value) > ... return list.__new__(cls, values) Did you really mean to setattr the class here? If I'm guessing your intenti

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Carl Banks
On Mar 31, 2:02 am, Rob Williscroft wrote: > Frank Millman wrote in news:mailman.1360.1270018159.23598.python- > l...@python.org in comp.lang.python: > > > I came up with a simple solution that seems to work - > > class MyTuple(tuple): > > ...   def __new__(cls, names, values): > > ...     fo

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Rob Williscroft
Frank Millman wrote in news:mailman.1360.1270018159.23598.python- l...@python.org in comp.lang.python: > I came up with a simple solution that seems to work - > class MyTuple(tuple): > ... def __new__(cls, names, values): > ... for name, value in zip(names, values): > ... setattr

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Bruno Desthuilliers
lbolla a écrit : class MyList(list): def __init__(self, names, values): list.__init__(self, values) for name, value in zip(names, values): setattr(self, name, value) names = ['A', 'B', 'C'] values = ['a', 'b', 'c'] lst = MyList(na

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Frank Millman
"lbolla" wrote in message news:f8011c0b-0b1b-4a4f-94ff-304c16ef9...@q16g2000yqq.googlegroups.com... On Mar 31, 7:49 am, "Frank Millman" wrote: Hi all When subclassing immutable types, you need to override __new__; otherwise you need to override __init__. Perfect. Thanks very much. Fra

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread lbolla
On Mar 31, 7:49 am, "Frank Millman" wrote: > Hi all > > I needed something similar to, but not quite the same as, > collections.namedtuple. > > The differences are that namedtuple requires the 'names' to be provided at > creation time, and then lends itself to creating multiple instances of > itse

Sublassing tuple works, subclassing list does not

2010-03-30 Thread Frank Millman
Hi all I needed something similar to, but not quite the same as, collections.namedtuple. The differences are that namedtuple requires the 'names' to be provided at creation time, and then lends itself to creating multiple instances of itself. I wanted a more generic class where I could suppl