Unbound methods of types

Steve Holden sholden at holdenweb.com
Sun Apr 29 19:16:23 EDT 2001


"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:

>>> type("")
<type 'string'>
>>> str(type(""))
"<type 'string'>"
>>> x = str(type("")).upper
>>> x()
"<TYPE 'STRING'>"
>>> x("A banana")
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
TypeError: upper requires exactly 0 arguments; 1 given

regards
 Steve





More information about the Python-list mailing list