Unbound methods of types

Ben Hutchings ben.hutchings at roundpoint.com
Sun Apr 29 21:42:19 EDT 2001


"Steve  Holden" <sholden at holdenweb.com> writes:

> "Ben Hutchings" <ben.hutchings at roundpoint.com> wrote in message
> news:uwv831f2r.fsf_-_ at roundpoint.com...
> > "Alex Martelli" <aleaxit at yahoo.com> writes:
> > <snip>
> > > # 4. special case, as you note, for string. stuff
> > > L = map(string.capitalize, l)
> > <snip>
> >
> > I feel that it ought to be possible to use type("").capitalize here,
> > as one could do with a class method.  Why is it not possible to get
> > unbound methods from types?  Should it be?
> >
> type("") is of type type, not type string. You can apply the str() function
> to it, but this will give you a method with the string instance bound to it:
<snip>

Yes, I know.  Why are you telling me this?

Let me try to explain myself more clearly.  If strings were instances
of a class String, I could say String.foo to get the unbound string
method foo; as it is, there happens to be a string module so I can say
string.foo to get a function that works as if it were the unbound
string method (except where foo = join).  For other sequences such as
lists, there is no such module to help.  To pick a better example, I
can't say:

    map(types.DictType.keys, [{'a':1,'b':2,'c':3}, {'d':4,'e':5,'f':6}])

and get back something like:

    [['b', 'c', 'a'], ['f', 'd', 'e']]

and there is no module dict with a function keys that would help me
here.  Instead I have to use 'lambda d: d.keys()'.  Actually, I could
just use a list comprehension here; but if I wanted an unbound type
method for some other purpose I'd have to resort to that lambda
expression.  I suppose this is just another problem of the old
type/class division.

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list