473,503 Members | 10,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Customizing sequence types

Mr.SpOOn wrote:
Hi,
I'm trying to create a class which inherit a list to change some behavior.
This list should contain other instance objects and has to manage
these instances in a particular way.

1) I need to sort this elements in this list, but they must be sorted
using an instance variable. What does Python use to sort elements? I
mean, there is a __sort__ method in the class list or it just uses
comparison operators? In this case, shall I just redefine this
operators in the element classes?
By default Python uses the built-in "cmp()" function to compare pairs of
elements during its sorts. You can use the "cmp=f" argument to sort to
replace it with your own function, but if this is a Python function the
comparison *will* be slower.

Alternatively for can provide a "key=f" argument; in this case before
beginning the sort, sort() will call the provided key function for each
element of the list your are trying to sort, and then it will compare
the generated keys instead of the list elements (using Python's built-in
"cmp()" function.
2) I need to have just unique elements. Maybe this is related to first
question (or maybe not). Again, to estabilish the uniqueness of an
element I have to use an instance variable. I think I have to rewrite
both the methods append() and __contains__(), but I'm not sure how.

For the first one I did:

def append(self, element):
if element in self:
pass
else:
list.append(self, element)

It seems right to me, but I have no idea what to do with __contains__()
Why do you need to change it? For speed?
Suggestion?
My suggestion would be to use Python's built-in sets, converting them to
lists when you need the sorted form.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 16 '08 #1
0 1107

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

Similar topics

8
2140
by: dan | last post by:
I was recently surprised, and quite shocked in fact, to find that Python treats negative indices into sequence types as if they were mod(length-of-sequence), at least up to -len(seq). This fact...
7
1499
by: Chinook | last post by:
OO approach to decision sequence? --------------------------------- In a recent thread (Cause for using objects?), Chris Smith replied with (in part): > If your table of photo data has...
8
14385
by: Dave Moore | last post by:
Is there any way to specify an istream separator sequence? For example, suppose I have a record consisting of a list of comma-separated values (no whitespace). I want to set the istream up so that...
0
1263
by: Anders Ljusberg | last post by:
Hi! I have a problem.. I'm trying to get the XML from a dataset to conform to an XSD, but it keeps rearranging some elements. Try this schema: <xs:schema id="test"...
4
6655
by: VR | last post by:
I am trying to embed a check box into a FlexGrid's cell, but having a problem when I start scrolling the grid. Here is my MyCheckBox class... class MyCheckBox : CheckBox { void Init (...
0
288
by: John Knoop | last post by:
Hi all! I have a dataset filled containing one row of data. Dataset.WriteXML gives me this: <Article> <Table> <aID>10</aID> <aHeader>Some text goes here</aHeader>...
3
1920
by: Daniel Wilson | last post by:
I am trying to read data from MS SQL Server and turn it into an XML message to send to a remote server, as follows. sfSchemaFileDiag.FilterIndex = 2 If sfSchemaFileDiag.ShowDialog =...
1
3015
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
4
2379
by: Rasika WIJAYARATNE | last post by:
Hi everyone, We have a web method that accepts a type of AuthHeader containing credentials for validating WS calls, which is shown below: public class AuthHeader : SoapHeader { public string...
9
2358
by: Mr.SpOOn | last post by:
Hi, I'm trying to create a class which inherit a list to change some behavior. This list should contain other instance objects and has to manage these instances in a particular way. 1) I need...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
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...
0
5562
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,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
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...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
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 ...
0
371
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...

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.