473,671 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subclassing list, what special methods do this?

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 above two
operations. From my testing, it seems to be __setslice__ for both,
but the docs say __setslice__ and brethren are deprecated. I would
have thought that __setitem__ and __delitem__ would be what was
called, but again, my testing says otherwise.
Jun 27 '08 #1
5 2195
En Fri, 13 Jun 2008 15:38:15 -0300, Mike Kent <mr******@cox.n etescribió:
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 above two
operations. From my testing, it seems to be __setslice__ for both,
but the docs say __setslice__ and brethren are deprecated. I would
have thought that __setitem__ and __delitem__ would be what was
called, but again, my testing says otherwise.
__setslice__ and __delslice__, *and* __setitem__/__delitem__ for extended
slice notation.
The first two are deprecated, but since the list type implements them you
have to do the same, because the interpreter invokes those methods when
they exist, even if found in a base class.

--
Gabriel Genellina

Jun 27 '08 #2
On Jun 13, 11:38 am, Mike Kent <mrmak...@cox.n etwrote:
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 above two
operations. From my testing, it seems to be __setslice__ for both,
but the docs say __setslice__ and brethren are deprecated. I would
have thought that __setitem__ and __delitem__ would be what was
called, but again, my testing says otherwise.
It is a combination. http://docs.python.org/ref/sequence-methods.html

For setting a slice in the form `x[from:to] = seq` the __setslice__
method will be called if it exists:
x.__setslice__( from, to, seq)

if __setslice__ doesn't exists __setitem__ is called with a slice
object:
x.__setitem__(s lice(from,to), seq)

if setting a slice in the form `x[from:to:step] = seq` (extended
slicing) __setitem__ will be called with a slice object:
x.__setitem__(s lice(from, to, step), seq)

For non slices (single index values) __setitem__ is always called.

The real problem, and someone correct me if I'm wrong (this is a
newsgroup... of course they will :p), is that you can't make your
class hide or get rid of __setslice__ on the base class. I've tried
making __getattribute_ _ raise an AttributeError and making __hasattr__
return False, neither works. I think this is because list is
implemented in C and the __setslice__ slot is filled. Because the list
object is implemented in C and is optimized, it doesn't use
__getattribute_ _ or __hasattr__ to find out if the slice method
exists, Python just grabs the pointer from the c structure and uses
it.

In my examples I only mentioned __setslice__/__setitem__, but all the
same should apply to __delslice__/__delitem__ as well.

So, it looks like as long as you want to subclass list, you are stuck
implementing both __*slice__ and __*item__ methods.

Matt
Jun 27 '08 #3
On Jun 13, 8:43*pm, Matimus <mccre...@gmail .comwrote:

...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.
Jun 27 '08 #4

"Matimus" <mc******@gmail .comwrote in message
news:20******** *************** ***********@27g 2000hsf.googleg roups.com...
| 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.

Jun 27 '08 #5
On Jun 13, 1:38*pm, Mike Kent <mrmak...@cox.n etwrote:
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 above two
operations. *From my testing, it seems to be __setslice__ for both,
but the docs say __setslice__ and brethren are deprecated. *I would
have thought that __setitem__ and __delitem__ would be what was
called, but again, my testing says otherwise.
Or you could forget this "subclassin g" stuff, and just implement
__setitem__, __getitem__, __delitem__, __len__, and __iter__ in your
own class, and not worry about what list does or doesn't do. You
could have your class contain a list to implement with, and then
delegate to it from your class's code.

Overall, I think Python as a language favors composition/delegation
over inheritance - c/d is so easy to do with __getattribute_ _, and
inheritance is largely unnecessary since type/interface is not checked
until runtime. Yet many O-O texts are written around C++ and Java's
static type models, so we see a preponderance of patterns and
conventional O-O wisdom recommending inheritance. For Python, it
ain't necessarily so...

-- Paul
Jun 27 '08 #6

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

Similar topics

16
2130
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 may be of some interest. To refresh your memory, I report here some quotation from that thread. Jacek Generowicz: > I sumbit to you that read-only closures are rare in Python because > they are a recent addition to the language.
13
1510
by: Chris Cioffi | last post by:
Hello all! I'm trying to subclass int such that once it reaches a certain value, it flips back to the starting count. This is for use as an X12 control number. Subclassing int and getting the display like I want it was trivial, however what's the best way to make it such that: 990 + 1 = 991 991 + 1 = 992 .... 998 + 1 = 999
4
1307
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 popular notation forms exist: products of disjoint cycles ("(0 1 3)(2 4)") and 2*length matrices with the identity permutation as the first line ( over ). I find the latter more appropriate, since it's
2
5155
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 class which is supposed to be a representation of a genome: class Genome(list): def __init__(self): list.__init__(self) self = ....
3
1691
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary parts computed in point()'s __init__ function with their values based on the arglist. I want to compute with point instances as though they were native complex numbers, but I want to be able to
6
3067
by: gregory lielens | last post by:
Hello, I am currently writing python bindings to an existing C++ library, and I just encountered a problem that I hope has been solved by more experienced python programmers: A C++ class (let's call it CClass) is binded using classical Python extension API to _PClass, which is accesible through python without any problem. The problem is that I want this class to be extended/extensible in python, and expose the python-extended version...
11
3818
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
16
2067
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do it with inheritance, a kind of "dynamic subclassing", without subclassing the class, only this instance "b" ! In fact, i've got my instance "b", and another class "MoreMethods"
2
1119
by: Kirk Strauser | last post by:
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: .... def __getitem__(self, index): .... return 5 ....
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8593
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7423
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.