473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Subclassing list the right way?

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 __getitem__(self, index):
.... return 5
....
>>a = Foo([1, 2, 3, 4, 5])
print a
[1, 2, 3, 4, 5]
>>print a[2:4]
[3, 4]
>>print a[3]
5

I first expected that to instead behave like:
>>print a
[5, 5, 5, 5, 5]
>>print a[2:4]
[5, 5]

Is there a "right" way to do this?
--
Kirk Strauser
Jun 27 '08 #1
2 1098
On Apr 25, 4:03*pm, Kirk Strauser <kirk.strau...@gmail.comwrote:
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 __getitem__(self, index):
... * * * * * * return 5
...>>a = Foo([1, 2, 3, 4, 5])
>print a
[1, 2, 3, 4, 5]
>print a[2:4]
[3, 4]
>print a[3]

5

I first expected that to instead behave like:
>print a
[5, 5, 5, 5, 5]
>print a[2:4]

[5, 5]

Is there a "right" way to do this?
--
Kirk Strauser
I'm not totally sure, but I think you have to implement __getslice__
as well, even if it is a deprecated magic function. My guess for this
is that list implements __getslice__ and when you ask for a slice,
Python checks for __getslice__ first, and then, if you don't have it,
calls __getitem__. And since list has it, it's what is called.

As for "print a", you have to override __str__ and __repr__ too.

As a side note, the second argument to __getitem__ is not index, it's
key, because this argument can either be an int index or a slice()
object.
Jun 27 '08 #2
On Apr 25, 7:03 am, Kirk Strauser <kirk.strau...@gmail.comwrote:
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 __getitem__(self, index):
... return 5
...>>a = Foo([1, 2, 3, 4, 5])
>print a
[1, 2, 3, 4, 5]
>print a[2:4]
[3, 4]
>print a[3]

5

I first expected that to instead behave like:
>print a
[5, 5, 5, 5, 5]
>print a[2:4]

[5, 5]

Is there a "right" way to do this?
--
Kirk Strauser
The built in types are very optimized. You can't make any assumptions
about how overriding methods on them will affect other method calls.

From the docs:

__getitem__( self, key)

Called to implement evaluation of self[key]. For sequence types, the
accepted keys should be integers and slice objects. Note that the
special interpretation of negative indexes (if the class wishes to
emulate a sequence type) is up to the __getitem__() method. If key is
of an inappropriate type, TypeError may be raised; if of a value
outside the set of indexes for the sequence (after any special
interpretation of negative values), IndexError should be raised. For
mapping types, if key is missing (not in the container), KeyError
should be raised. Note: for loops expect that an IndexError will be
raised for illegal indexes to allow proper detection of the end of the
sequence.

What that basicly says is that it affects what is returned by a[x].
>>class Foo(list):
.... def __getitem__(self, index):
.... return 5
....
>>a = Foo([1, 2, 3, 4, 5])
print a # a.__str__ called
[1, 2, 3, 4, 5]
>>print a[2:4] # a.__getslice__ called
[3, 4]
>>print a[3] # a.__getitem__ called
5

The _right_ way? Well, it depends on what you really want to do. If
you really want to calculate the value at call time but have it behave
exactly as a list in every way, then you will have to override pretty
much any method that deals with the values normally stored in the list
object. That means any method that either returns values stored in the
object, accepts as a parameter values stored in the object or uses the
values stored in the object in any way.

Methods that return values stored in the object:
__getitem__
__getslice__

pop

Methods that accept as a parameter values stored in the object:
__contains__
count
remove
index

Methods that use values stored in the object in some way:
__add__
__eq__
__ge__
__gt__
__iadd__
__imul__
__le__
__lt__
__mul__
__ne__
__reduce__
__reduce_ex__? I'm not sure about this one, I think it is for pickling
__repr__
__reversed__
__rmul__
__str__
__iter__
sort
reverse

I know that Py3.0 is going to implement Abstract Base Classes. I don't
know if that is going to clean up this behavior or not. So, you can
either write a lot of code or restrict the way you use lists to a
limited amount of functionality. There is a third option, which is to
calculate the values when storing. This might be a little easier, but
if you want to ensure that values returned by all methods are the same
type as your class you are back to overriding a lot of methods.

Matt
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Michele Simionato | last post by:
I have read with interest the recent thread about closures. The funny thing is that the authors are arguing one against the other but I actually agree with all of them and I have a proposal that...
4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
4
by: Jonas Koelker | last post by:
Hello there. I'm new to this list (/me welcomes himself). anyways, I'm having doubts about how to subclass properly. The big picture is that I want to handle permutations. As we all know, two...
3
by: Emiliano Molina | last post by:
Where can I find information on how to do this? Specifically I am concerned about something like: class NewList(list): def __init__(self): list.__init__(self) The above code does not allow...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
2
by: Uwe Mayer | last post by:
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...
12
by: Jane Austine | last post by:
Please see the following code: -------------------------------- class rev_wrap(object): def __init__(self,l): self.l=l def __getitem__(self,i): return self.l class rev_subclass(list): def...
5
by: Pieter Linden | last post by:
Hi, This question refers sort of to Rebecca Riordan's article on Access web about subclassing entities: http://www.mvps.org/access/tables/tbl0013.htm How practical is this? I am writing a...
5
by: Mike Kent | last post by:
For Python 2.5 and new-style classes, what special method is called for mylist = seq and for del mylist (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.