[Python-bugs-list] [ python-Feature Requests-517371 ] Add .count() method to tuples

noreply@sourceforge.net noreply@sourceforge.net
Wed, 27 Mar 2002 02:24:30 -0800


Feature Requests item #517371, was opened at 2002-02-14 08:21
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=517371&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Nobody/Anonymous (nobody)
Summary: Add .count() method to tuples

Initial Comment:
Tuples have every method afforded to lists except for 
those which mutate the list; however, there is one 
exception:  .count() appears to have been left out 
eventhough it can be well-defined for tuples as well 
as lists.

>>> s = 'the trump'
>>> s.count('t')
2
>>> list(s).count('t')
2
>>> tuple(s).count('t')
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in ?
    tuple(s).count('t')
AttributeError: 'tuple' object has no attribute 'count'


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2002-03-27 10:24

Message:
Logged In: YES 
user_id=80475

IMHO, the forces pushing toward using lists versus tuples 
should be immutability and performance rather than the 
absence of methods.

Even if being pushed were a feature, a more important 
feature is substitutability -- the ability to develop a 
program using lists and then make a late design decision 
that tuples would have been a better choice.  

Substitutability is unnecessarily impaired by the absence 
of .index() and .count().

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2002-02-14 19:33

Message:
Logged In: YES 
user_id=31435

Guido has rejected this idea before, so don't hold your 
breath.  tuples and lists are intended to be used in 
different ways, and it's "a feature" that their differing 
methods push you toward using them as intended.

Note that tuples don't support .index() either, and that's 
also intentional.

Note that you can use the operator.countOf() function on 
tuples (and, in 2.2, on any iterable object).

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=517371&group_id=5470