Sublassing tuple works, subclassing list does not

2010-03-31 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

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread lbolla
On Mar 31, 7:49 am, Frank Millman fr...@chagford.com 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

Re: Sublassing tuple works, subclassing list does not

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

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 =

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(cls,

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Carl Banks
On Mar 31, 2:02 am, Rob Williscroft r...@rtw.me.uk 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): ...    

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Hrvoje Niksic
Frank Millman fr...@chagford.com 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

Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread Frank Millman
On Mar 31, 8:49 am, Frank Millman fr...@chagford.com 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

Re: Subclassing list and splicing

2009-09-04 Thread Nobody
On Thu, 03 Sep 2009 17:10:12 +, Kreso wrote: I would prefer that resulting object m belonged to myclist class. How to obtain such behaviour? Must I somehow implement __getslice__ method myself? Yes; you should also implement __getitem__(), as this is used for extended slices. --

Subclassing list and splicing

2009-09-03 Thread Kreso
I am subclassing list class and it basically works, but I don't understand why after splicing these mylist objects I don't get returned mylist objects. What I get are list objects: class mylist(list): def __init__(self): list.__init__(self) k = mylist() k.append(1) k.append(2

Re: Subclassing list and splicing

2009-09-03 Thread Emile van Sebille
On 9/3/2009 10:10 AM Kreso said... I am subclassing list class and it basically works, but I don't understand why after splicing these mylist objects I don't get returned mylist objects. What I get are list objects: snip I would prefer that resulting object m belonged to myclist class. How

Re: Subclassing list and splicing

2009-09-03 Thread Kreso
Kreso kknowayu...@donoevil.com wrote: [...] I would prefer that resulting object m belonged to myclist class. I forgot to add that mylist instances in my case have some attributes (that's why I need special container class in the first place) which should be preserved after splicing. In my

subclassing 'list'

2009-01-06 Thread akineko
Hello everyone, I'm creating a class which is subclassed from list (Bulit-in type). It works great. However, I'm having a hard time finding a way to set a new value to the object (within the class). There are methods that alter a part of the object (ex. __setitem__()). But I couldn't find any

Re: subclassing 'list'

2009-01-06 Thread Chris Rebert
On Tue, Jan 6, 2009 at 12:34 PM, akineko akin...@gmail.com wrote: Hello everyone, I'm creating a class which is subclassed from list (Bulit-in type). It works great. However, I'm having a hard time finding a way to set a new value to the object (within the class). There are methods that

Re: subclassing 'list'

2009-01-06 Thread James Stroud
akineko wrote: Hello everyone, I'm creating a class which is subclassed from list (Bulit-in type). It works great. However, I'm having a hard time finding a way to set a new value to the object (within the class). There are methods that alter a part of the object (ex. __setitem__()). But I

Re: subclassing 'list'

2009-01-06 Thread J. Cliff Dyer
On Tue, 2009-01-06 at 12:34 -0800, akineko wrote: Hello everyone, I'm creating a class which is subclassed from list (Bulit-in type). It works great. However, I'm having a hard time finding a way to set a new value to the object (within the class). There are methods that alter a part of

Re: subclassing 'list'

2009-01-06 Thread akineko
Hello Chris and James, Thank you for you guys' prompt response. Yes, that is what I wanted to do. I, somehow, didn't think of using those list methods. Instead, I was looking for a single method to override. Big Thanks! Aki- On Jan 6, 12:43 pm, Chris Rebert c...@rebertia.com wrote: If you

Re: python bug when subclassing list?

2008-11-11 Thread Ethan Furman
Hamish McKenzie wrote: I want to write a Vector class and it makes the most sense to just subclass list. I also want to be able to instantiate a vector using either: Vector( 1, 2, 3 ) OR Vector( [1, 2, 3] ) so I have this: class Vector(list): def __new__( cls, *a ): try:

Re: python bug when subclassing list?

2008-11-11 Thread Steve Holden
Hamish McKenzie wrote: I want to write a Vector class and it makes the most sense to just subclass list. I also want to be able to instantiate a vector using either: Vector( 1, 2, 3 ) OR Vector( [1, 2, 3] ) so I have this: class Vector(list): def

python bug when subclassing list?

2008-11-06 Thread Hamish McKenzie
I want to write a Vector class and it makes the most sense to just subclass list. I also want to be able to instantiate a vector using either: Vector( 1, 2, 3 ) OR Vector( [1, 2, 3] ) so I have this: class Vector(list): def __new__( cls, *a ): try:

python bug when subclassing list?

2008-11-06 Thread Hamish McKenzie
I want to write a Vector class and it makes the most sense to just subclass list. I also want to be able to instantiate a vector using either: Vector( 1, 2, 3 ) OR Vector( [1, 2, 3] ) so I have this: class Vector(list): def __new__( cls, *a ): try: print a

Re: python bug when subclassing list?

2008-11-06 Thread Arnaud Delobelle
Hamish McKenzie [EMAIL PROTECTED] writes: I want to write a Vector class and it makes the most sense to just subclass list. I also want to be able to instantiate a vector using either: Vector( 1, 2, 3 ) OR Vector( [1, 2, 3] ) so I have this: class Vector(list): def __new__(

Re: Subclassing list, what special methods do this?

2008-06-14 Thread Mike Kent
On Jun 13, 8:43 pm, Matimus [EMAIL PROTECTED] wrote: ...chop... So, it looks like as long as you want to subclass list, you are stuck implementing both __*slice__ and __*item__ methods. Matt Thanks. That was clear and concise, just what I needed. --

Re: Subclassing list, what special methods do this?

2008-06-14 Thread Terry Reedy
Matimus [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | So, it looks like as long as you want to subclass list, you are stuck | implementing both __*slice__ and __*item__ methods. Unless writing in 3.0, where they have finally disappeared. --

Re: Subclassing list, what special methods do this?

2008-06-14 Thread Paul McGuire
On Jun 13, 1:38 pm, Mike Kent [EMAIL PROTECTED] wrote: For Python 2.5 and new-style classes, what special method is called for mylist[2:4] = seq and for del mylist[2:4] (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and I'm having trouble determining

Subclassing list, what special methods do this?

2008-06-13 Thread Mike Kent
For Python 2.5 and new-style classes, what special method is called for mylist[2:4] = seq and for del mylist[2:4] (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and I'm having trouble determining what special methods I have to override in my class for the

Re: Subclassing list, what special methods do this?

2008-06-13 Thread Gabriel Genellina
En Fri, 13 Jun 2008 15:38:15 -0300, Mike Kent [EMAIL PROTECTED] escribió: For Python 2.5 and new-style classes, what special method is called for mylist[2:4] = seq and for del mylist[2:4] (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and I'm having

Re: Subclassing list, what special methods do this?

2008-06-13 Thread Matimus
On Jun 13, 11:38 am, Mike Kent [EMAIL PROTECTED] wrote: For Python 2.5 and new-style classes, what special method is called for mylist[2:4] = seq and for del mylist[2:4] (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and I'm having trouble determining

Subclassing list the right way?

2008-04-25 Thread Kirk Strauser
I want to subclass list so that each value in it is calculated at call time. I had initially thought I could do that by defining my own __getitem__, but 1) apparently that's deprecated (although I can't find that; got a link?), and 2) it doesn't work. For example: class Foo(list): ... def

Re: Subclassing list the right way?

2008-04-25 Thread Virgil Dupras
On Apr 25, 4:03 pm, Kirk Strauser [EMAIL PROTECTED] wrote: I want to subclass list so that each value in it is calculated at call time.  I had initially thought I could do that by defining my own __getitem__, but 1) apparently that's deprecated (although I can't find that; got a link?), and 2)

Re: Subclassing list the right way?

2008-04-25 Thread Matimus
On Apr 25, 7:03 am, Kirk Strauser [EMAIL PROTECTED] wrote: I want to subclass list so that each value in it is calculated at call time. I had initially thought I could do that by defining my own __getitem__, but 1) apparently that's deprecated (although I can't find that; got a link?), and 2)

subclassing list question

2007-05-20 Thread manstey
Hi, I have a simple class that subclasses list: class CaListOfObj(list): subclass of list def __init__(self, *args, **kwargs): list.__init__(self, *args, **kwargs) a = CaListOfObj([1,2,3]) I want an instance method to be run EVERY time a is modified. Is this possible?

subclassing list question

2007-05-20 Thread manstey
Hi, I have a simple class that subclasses a list: class CaListOfObj(list): subclass of list def __init__(self, *args, **kwargs): list.__init__(self, *args, **kwargs) a= CaListOfObj([1,2,3]) Is it possible to have a method in the class that is called EVERY time a is modified?

subclassing list question

2007-05-20 Thread manstey
test -- http://mail.python.org/mailman/listinfo/python-list

Re: TypeError when subclassing 'list'

2006-02-27 Thread looping
Gerard Flanagan wrote: Hello all Could anyone shed any light on the following Exception? The code which caused it is below. Uncommenting the 'super' call in 'XmlNode' gives the same error. If I make XmlNode a subclass of 'object' rather than 'list' then the code will run. Thanks in

Re: TypeError when subclassing 'list'

2006-02-27 Thread Gerard Flanagan
Simon Percivall wrote: The error you're seeing is because you've rebound 'list' to something else. Try putting list = type([]) somewhere above your code. That's it! I had rebound 'list' earlier (in error), and though I deleted the code it must have been remembered somehow. Restarting

TypeError when subclassing 'list'

2006-02-26 Thread Gerard Flanagan
Hello all Could anyone shed any light on the following Exception? The code which caused it is below. Uncommenting the 'super' call in 'XmlNode' gives the same error. If I make XmlNode a subclass of 'object' rather than 'list' then the code will run. Thanks in advance. Exception: Traceback

Re: TypeError when subclassing 'list'

2006-02-26 Thread Steve Juranich
Gerard Flanagan wrote: Hello all Could anyone shed any light on the following Exception? The code which caused it is below. Uncommenting the 'super' call in 'XmlNode' gives the same error. If I make XmlNode a subclass of 'object' rather than 'list' then the code will run. ... Code:

Re: TypeError when subclassing 'list'

2006-02-26 Thread Gerard Flanagan
Steve Juranich wrote: Gerard Flanagan wrote: Hello all Could anyone shed any light on the following Exception? The code which caused it is below. Uncommenting the 'super' call in 'XmlNode' gives the same error. If I make XmlNode a subclass of 'object' rather than 'list' then the

Re: TypeError when subclassing 'list'

2006-02-26 Thread Simon Percivall
The error you're seeing is because you've rebound 'list' to something else. Try putting list = type([]) somewhere above your code. -- http://mail.python.org/mailman/listinfo/python-list

subclassing list

2005-07-31 Thread spike
I've googled like crazy and can't seem to find an answer to why this isn't working. I want to create a custom list class that acts as a circular list. ie: my_list = (0, 1, 2) how I want it to behave: my_list[0] - 0 my_list[1] - 1 my_list[2] - 2 my_list[3] - 0 my_list[4] - 1 ...etc so, what

Re: subclassing list

2005-07-31 Thread Robert Kern
spike wrote: I've googled like crazy and can't seem to find an answer to why this isn't working. I want to create a custom list class that acts as a circular list. ie: my_list = (0, 1, 2) how I want it to behave: my_list[0] - 0 my_list[1] - 1 my_list[2] - 2 my_list[3] - 0

Re: subclassing list

2005-07-31 Thread John Machin
spike wrote: I've googled like crazy and can't seem to find an answer to why this isn't working. I want to create a custom list class that acts as a circular list. ie: my_list = (0, 1, 2) Perhaps you mean [0, 1, 2] how I want it to behave: my_list[0] - 0 my_list[1] - 1 my_list[2]

Re: subclassing list

2005-07-31 Thread spike grobstein
You also need to post the code that raises the error, or no one else can debug it. sorry, I thought I had pasted that line in there, but I guess I missed it. Here's the full code (after modifying it slightly based on your post): #! /usr/bin/env python def circular_list(list): def

Re: subclassing list

2005-07-31 Thread Robert Kern
spike grobstein wrote: You also need to post the code that raises the error, or no one else can debug it. sorry, I thought I had pasted that line in there, but I guess I missed it. Here's the full code (after modifying it slightly based on your post): #! /usr/bin/env python def

subclassing list

2004-12-23 Thread Uwe Mayer
Hi, I want to subclass list. The documentation states to prefer subclassing list instead of UserList. How to you clear the contents of a list subclass without creating a new object? Thanks in advance Uwe -- http://mail.python.org/mailman/listinfo/python-list

Re: subclassing list

2004-12-23 Thread Fuzzyman
class newList(list): def clear(self): self[:] = [] is one way. HTH Regards, Fuzzy http://www.voidspace.org.uk/python/index.shtml -- http://mail.python.org/mailman/listinfo/python-list

Re: subclassing list

2004-12-23 Thread Richie Hindle
[Uwe] How [do] you clear the contents of a list subclass without creating a new object? Use the paranoia emoticon: del x[:]. For example: class L(list): ... pass ... x = L() x.append(Spam) del x[:] x [] type(x) class '__main__.L' with-thanks-to-Gordon-McMillan-ly y'rs, -- Richie