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
"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
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
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
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
"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
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
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
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.
--
http:
Kreso 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 simple understaning of pyth
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:
I would prefer that resulting object m belonged to myclist cla
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)
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" wrote:
> If you mean you want to r
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
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 coul
On Tue, Jan 6, 2009 at 12:34 PM, 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 pa
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 met
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)
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:
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):
>
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
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:
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 determini
"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.
--
http://mail.python.org/mailman/li
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.
--
http://mail.python.org/mailman/listi
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 determin
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 trou
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 abo
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?), a
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?), a
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):
... de
test
--
http://mail.python.org/mailman/listinfo/python-list
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 modif
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?
T
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
Pyt
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
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
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
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:
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 (mos
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
>
>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 __getitem_
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
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[
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, wha
[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)
>>>
with-thanks-to-Gordon-McMillan-ly y'rs,
--
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
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
48 matches
Mail list logo