str vs dict API size (was 'Re: left padding zeroes on a string...')

Bengt Richter bokr at oz.net
Fri Mar 25 23:58:20 EST 2005


On Sat, 26 Mar 2005 04:10:21 GMT, Ron_Adam <radam2 at tampabay.rr.com> wrote:

>On Fri, 25 Mar 2005 18:06:11 -0500, "George Sakkis"
><gsakkis at rutgers.edu> wrote:
>
>>
>>I'm getting off-topic here, but it strikes me that strings have so many methods (some of which are
>>of arguable utility, e.g. swapcase), while proposing two useful methods (http://tinyurl.com/5nv66)
>>for dicts -- a builtin with a considerably smaller API than str -- meets so much resistance. Any
>>insight ?
>>
>>George
>>
>
>I did a quick check.
>
>>>> len(dir(str))
>63
>>>> len(dir(int))
>53
>>>> len(dir(float))
>45
>>>> len(dir(dict))
>40
>>>> len(dir(list))
>42
>>>> len(dir(tuple))
>27
>
>We need more tuple methods!  jk  ;)
>
>Looks like the data types, strings, int an float; have more methods
>than dict, list, and tuple. I would expect that because there is more
>ways to manipulate data than is needed to manage containers.
>
More data:

 >>> for n,k in sorted((len(dir(v)),k) for k,v in ((k,v) for k,v in vars(__builtins__).items()
 ...     if isinstance(v, type))): print '%4s: %s' %(n,k)
 ...
   12: basestring
   12: object
   13: classmethod
   13: staticmethod
   14: enumerate
   15: reversed
   16: super
   16: xrange
   17: slice
   18: property
   23: buffer
   27: tuple
   27: type
   34: file
   34: open
   37: frozenset
   40: dict
   42: list
   45: float
   48: complex
   50: set
   53: bool
   53: int
   53: long
   60: unicode
   63: str

Hm, I guess that includes inheritance, and they should be callable, so maybe (not researched)

 >>> for n,k in sorted((sum(callable(m) for k,m in vars(v).items()),k)
 ...     for k,v in ((k,v) for k,v in vars(__builtins__).items()
 ...         if isinstance(v, type))): print '%4s: %s' %(n,k)
 ...
    1: basestring
    4: classmethod
    4: enumerate
    4: staticmethod
    5: reversed
    5: super
    6: property
    6: slice
    7: xrange
    9: bool
   10: object
   10: type
   16: buffer
   19: tuple
   22: file
   22: open
   30: frozenset
   33: dict
   35: list
   38: float
   39: complex
   44: set
   46: int
   46: long
   53: unicode
   56: str

Regards,
Bengt Richter



More information about the Python-list mailing list