473,722 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"index" method only for mutable sequences??

I was looking for a function or method that would return the index to the first
matching element in a list. Coming from a C++ STL background, I thought it might
be called "find". My first stop was the Sequence Types page of the Library
Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search
of the Library Reference's index seemed to confirm that the function did not
exist. A little later I realized it might be called "index" instead. Voila.

My point is that the docs list and describe it as a method that only exists for
MUTABLE sequences. Why only for mutables? The class of objects I would expect it
to cover would be all ordered sequences, or, to phrase it a little more
pointedly, anything that supports ordered INDEXing. My understanding is that
dict's don't fall into that class of objects since their ordering is not
documented or to be depended on. However, tuple's do support ordered indexing,
so why don't tuple's have an index method?

P.S.: I know I haven't yet gotten an answer to my "why" question yet, but,
assuming it's just an oversight or an example of design without the big picture
in mind, an added benefit to fixing that oversight would be that the "index"
method's documentation could be moved from the currently odd seeming location on
the "Mutable Sequence Types" page to a place someone would look for it logically.

P.P.S.: As much as the elementary nature of my question would make it seem, this
isn't my first day using Python. I've used it on and off for several years and I
LOVE Python. It is only because of my love for the language that I question its
ways, so please don't be overly defensive when I guess that the cause for this
possible oversight is a lack of design.

Corey Lubin
Apr 6 '07 #1
122 5502

C.L. wrote:
I was looking for a function or method that would return the index to the first
matching element in a list. Coming from a C++ STL background, I thought it might
be called "find". My first stop was the Sequence Types page of the Library
Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search
of the Library Reference's index seemed to confirm that the function did not
exist. A little later I realized it might be called "index" instead. Voila.

My point is that the docs list and describe it as a method that only exists for
MUTABLE sequences. Why only for mutables? The class of objects I would expect it
to cover would be all ordered sequences, or, to phrase it a little more
pointedly, anything that supports ordered INDEXing. My understanding is that
dict's don't fall into that class of objects since their ordering is not
documented or to be depended on. However, tuple's do support ordered indexing,
so why don't tuple's have an index method?

P.S.: I know I haven't yet gotten an answer to my "why" question yet, but,
assuming it's just an oversight or an example of design without the big picture
in mind, an added benefit to fixing that oversight would be that the "index"
method's documentation could be moved from the currently odd seeming location on
the "Mutable Sequence Types" page to a place someone would look for it logically.

P.P.S.: As much as the elementary nature of my question would make it seem, this
isn't my first day using Python. I've used it on and off for several years and I
LOVE Python. It is only because of my love for the language that I question its
ways, so please don't be overly defensive when I guess that the cause for this
possible oversight is a lack of design.

Corey Lubin
Looking around google a little bit, people have been asking that same
questions since at least 1992. Here is what the BDFL has to say:

Guido van Rossum (Gu************ **@cwi.nl)
Wed, 04 Dec 91 18:48:34 +0100
>In reply to: Steven D. Majewski: "Why no index for tuples or strings ?"

Most of the functions that operate on mutable sequences but NOT on
immutable ones are obviously there because they DO CHANGE the sequence.
BUT: why no string.index() or tuple.index() ?

Is this just an oversight ?
If not, what is the reason?
Umm, there isn't a real good reason. One thing I can say in my
defense is that string and tuple objects have no methods at all, all
operations on these are done with built-in operations like "+" and
"[...]", so adding an "index" method would be a bit of a change in the
structure.

For tuples, I suspect such a function would rarely be used; I think
that is most cases where x.index() would be useful, x is generally a
list, whose contents varies in time, rather than a tuple (which cannot
change easily).

For strings, there is a built-in module "string" which exports a
function "index" which searches for substrings, so you can say

string.index('o ne two three', 'two')

--Guido van Rossum, CWI, Amsterdam <gu***@cwi.nl >

Apr 6 '07 #2
C.L. wrote:
I was looking for a function or method that would return the index to the first
matching element in a list. Coming from a C++ STL background, I thought it might
be called "find". My first stop was the Sequence Types page of the Library
Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search
of the Library Reference's index seemed to confirm that the function did not
exist. A little later I realized it might be called "index" instead. Voila.

My point is that the docs list and describe it as a method that only exists for
MUTABLE sequences. Why only for mutables? The class of objects I would expect it
to cover would be all ordered sequences, or, to phrase it a little more
pointedly, anything that supports ordered INDEXing. My understanding is that
dict's don't fall into that class of objects since their ordering is not
documented or to be depended on. However, tuple's do support ordered indexing,
so why don't tuple's have an index method?

P.S.: I know I haven't yet gotten an answer to my "why" question yet, but,
assuming it's just an oversight or an example of design without the big picture
in mind, an added benefit to fixing that oversight would be that the "index"
method's documentation could be moved from the currently odd seeming location on
the "Mutable Sequence Types" page to a place someone would look for it logically.

P.P.S.: As much as the elementary nature of my question would make it seem, this
isn't my first day using Python. I've used it on and off for several years and I
LOVE Python. It is only because of my love for the language that I question its
ways, so please don't be overly defensive when I guess that the cause for this
possible oversight is a lack of design.

Corey Lubin

The amount of typing wasted to defend design decisions such as this can
boggle one's mind. Just use lists unless you have on overwhelming reason
to do otherwise.

James
Apr 6 '07 #3
James Stroud <jstroud <atmbi.ucla.edu writes:
>
C.L. wrote:
I was looking for a function or method that would return the index to the
first matching element in a list. ...
... __please don't be overly defensive__ ...

The amount of typing wasted to defend design decisions such as this can
boggle one's mind. Just use lists unless you have on overwhelming reason
to do otherwise.

James

Read the quote. I *am* using a list.

That doesn't change the fact that this is unfriendly design. It's an ugly
inconsistent chunk of a Python's past in which built-in types didn't behave like
objects. It sticks out like a sore thumb, maybe just not very often.

Oh, and thanks for the insulting tone of your anticipated response. Have you
anything better to do with your time than wasting bytes writing empty responses
to what you already deem a waste of typing?

*sighs* just what I expected: another idle troll defending something just for
the sake of defending it. On the other hand, thanks 7stud, for the truly helpful
response.

Apr 6 '07 #4
C.L. wrote:
James Stroud <jstroud <atmbi.ucla.edu writes:
>C.L. wrote:
>>I was looking for a function or method that would return the index to the
first matching element in a list. ...
... __please don't be overly defensive__ ...
The amount of typing wasted to defend design decisions such as this can
boggle one's mind. Just use lists unless you have on overwhelming reason
to do otherwise.

James


Read the quote. I *am* using a list.

That doesn't change the fact that this is unfriendly design. It's an ugly
inconsistent chunk of a Python's past in which built-in types didn't behave like
objects. It sticks out like a sore thumb, maybe just not very often.
OK, if you want a *reason*, the *reason* is that tuples were originally
intended to be used in the same way that tuples are used in mathematics:
as an ordered collection of dissimilar objects. Given that the sequence
held by a tuple wasn't intended to be homogeneous it didn't originally
make sense to be able to find something (which would of necessity be of
a particular type) in it.

Of course much has changed since then, and nowadays the world goes in
for tuple-abuse. Consequently the majority don't appear to understand
why tuple doesn't become simply an immutable list. But you have clearly
found the preferred solution on your own, so this is basically just a
history lesson.

Glad you asked? Is your thumb any less sore.
Oh, and thanks for the insulting tone of your anticipated response. Have you
anything better to do with your time than wasting bytes writing empty responses
to what you already deem a waste of typing?
I'd have thought you would have saved time simply by refusing to rise to
what you clearly see as bait.
*sighs* just what I expected: another idle troll defending something just for
the sake of defending it. On the other hand, thanks 7stud, for the truly helpful
response.
Get over it. This is Usenet, abuse is next door.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 6 '07 #5
C.L. wrote:
>
That doesn't change the fact that this is unfriendly design. It's an ugly
inconsistent chunk of a Python's past in which built-in types didn't behave like
objects. It sticks out like a sore thumb, maybe just not very often.
When this topic last appeared on my radar, I ended up writing a long
message about it:

http://groups.google.com/group/comp....e89128bdeb59c0

[...]
*sighs* just what I expected: another idle troll defending something just for
the sake of defending it. On the other hand, thanks 7stud, for the truly helpful
response.
The problem with 7stud's quote from GvR is that it's out of date:
tuples do have methods now, as you've noticed, but just not the index
method. Previously, I've missed that method, and it wouldn't be hard
to add it to the tuple class (in CPython's own source code), but one
has to wonder whether it's really necessary, or at least as necessary
as for other classes. Certainly, there's a trade-off between essential
functionality and having, say, 100 methods which are all useful to
someone but which make interactive introspection a rather tedious and
confusing business.

Paul

Apr 6 '07 #6
On Apr 6, 7:56 am, "Paul Boddie" <p...@boddie.or g.ukwrote:
The problem with 7stud's quote from GvR is that it's out of date:
I would argue that it shows the very guy who invented the language
stated publicly there was no good reason for tuples not to have an
index method---except for consistency; tuples had no other methods.
Now that tuples have other methods, the only justification he stated
no longer exists.
Apr 6 '07 #7
C.L. wrote:
James Stroud <jstroud <atmbi.ucla.edu writes:
>C.L. wrote:
>>I was looking for a function or method that would return the index to the
first matching element in a list. ...
... __please don't be overly defensive__ ...
The amount of typing wasted to defend design decisions such as this can
boggle one's mind. Just use lists unless you have on overwhelming reason
to do otherwise.

James


Read the quote. I *am* using a list.

That doesn't change the fact that this is unfriendly design. It's an ugly
inconsistent chunk of a Python's past in which built-in types didn't behave like
objects. It sticks out like a sore thumb, maybe just not very often.

Oh, and thanks for the insulting tone of your anticipated response. Have you
anything better to do with your time than wasting bytes writing empty responses
to what you already deem a waste of typing?

*sighs* just what I expected: another idle troll defending something just for
the sake of defending it. On the other hand, thanks 7stud, for the truly helpful
response.
I think you misinterpreted my post, I agree with you. Please read it
again. You have touched on a very old topic. Many people have fought
tooth and nail to defend arbitrary design decisions such as a tuple not
having an index. It boils down to the fact that tuples are useless as a
result unless you know you really need them--and you never really NEED them.

James
Apr 6 '07 #8
On Fri, 2007-04-06 at 11:33 -0700, 7stud wrote:
On Apr 6, 7:56 am, "Paul Boddie" <p...@boddie.or g.ukwrote:
The problem with 7stud's quote from GvR is that it's out of date:

I would argue that it shows the very guy who invented the language
stated publicly there was no good reason for tuples not to have an
index method---except for consistency; tuples had no other methods.
Now that tuples have other methods, the only justification he stated
no longer exists.
Except that that wasn't the only justification. GvR also said:

"""
For tuples, I suspect such a function would rarely be used; I think
that is most cases where x.index() would be useful, x is generally a
list, whose contents varies in time, rather than a tuple (which cannot
change easily).
"""

The lack of convincing use cases is still a pertinent reason today. Note
that the original poster on this thread did not present a use case for
tuple.index, they were only asking out of curiosity.

If you have a use case for tuple.index, please show it to me, and I'll
show you what you should be using instead of a tuple.

-Carsten
Apr 6 '07 #9
On Apr 6, 1:24 pm, Carsten Haese <cars...@uniqsy s.comwrote:
Except that that wasn't the only justification. GvR also said:

"""
For tuples, I suspect such a function would rarely be used; I think
that is most cases where x.index() would be useful, x is generally a
list, whose contents varies in time, rather than a tuple (which cannot
change easily).
"""
Touche.
Apr 7 '07 #10

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

Similar topics

1
10779
by: kim | last post by:
Scenario (I'm a newbie): I have a datagrid with countries listed and 5 parameters in each row. I want to add a row to this datagrid via an Event Handler. Very basic stuff. This method then call a Business method, which calls a Data method which calls an SP in MS SQL. My code compiles well, but the page throws "Index was outside the bounds of the array". I really can't figure why, take a look maybe you pros see can read between the...
2
1659
by: Tor Inge Rislaa | last post by:
Name of "index.aspx" Hi I have just published my first ASP.NET home page, but as I always does with HTML (index.htm) files I named the first webform to appear on the domain index.aspx but It doesn't show. If I type the full URL it works fine.
1
2533
by: Clark Choi | last post by:
I ran the sample application called Petstore from msdn. Everything went fine until I tested Update button on the web form to update the database through Datagrid. I have been searching the web to track down this error, but none of them really explain this problem/solution very well. I tried hard to debug this, but debuggin itself was impossible. The error message is as follows:
1
1934
by: Dave | last post by:
I'm getting the following error on an EditCommand event. This code is running on production web farm and the thing is it doesn't happen all the time. If I get this error, click the back button, and try again it works. Sometimes it works the first time in. I tried to step through the debugger running locally but I can't duplicate it. Server Error in '/' Application. ---------------------------------------- Index was out of range. Must...
4
3607
by: Antoine | last post by:
Herfried and Cor:- I used tracing and actually tracked down the code that was causing the problem most likely. I wonder if you wanted to comment on it. Also I wonder if there is a better way of testing if there is data than testing the length of the xml string I used as stringreader to create the dataset, but thats a side issue. I think I tried isdbnull and is nothing and stuff like that and they cause
15
4478
by: bill | last post by:
I am trying to write clean code but keep having trouble deciding when to quote an array index and when not to. sometimes when I quote an array index inside of double quotes I get an error about enased whitespace (to my best memory) AT other times I get an undefined index notice as below: Notice: Undefined index: last_reminder_id in...
5
7364
by: Pseudonyme | last post by:
Dear All : Ever had an httpd error_log bigger than the httpd access log ? We are using Linux-Apache-Fedora-Httpd 2006 configuration. The PHP lines code that lead too tons of errors are : $http_ref= $HTTP_REFERER; $prog = $_COOKIE;
1
2188
BeemerBiker
by: BeemerBiker | last post by:
Using GridView and SqlDataSource I found that an index field defaults to "ReadOnly=True" and I have to change it to False and then hide it before my sql UPDATE command works. This seems awkward and I was wondering if there was a better way of getting a GridView to work with MS SQL. GridView1: -SelectCommand = "SELECT * FROM xxx" -UpdateCommand="UPDATE xxx SET yyy WHERE =@index; The SELECT gets "index". If I explicitly code the select so...
0
1673
BeemerBiker
by: BeemerBiker | last post by:
I was wondering if a command like "SqlDataSource1.insert()" can somehow give me the new index that was created when the insert took place. If the insert always occurs at the end of the dataset then I assume something like "...Count" could be used to figure out where the insert is going to take place. I am not sure if there is a "Count" in SqlDataSource. Unfortunately, I didnt see a callback for something like "ItemInserted" which would give...
0
8860
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9382
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
6681
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
5995
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
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
3207
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
2601
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
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.