Strange error with unbound method
Remco Gerlich
scarblac at pino.selwerd.nl
Mon Apr 16 12:32:51 EDT 2001
Fernando RodrÃguez <spamers at must.die> wrote in comp.lang.python:
> I have a class called ParaStyle (see code below). This class has a
> method called makeBlackAndWhite that returns a new instance of class
> ParaStyle.
>
> I want to transform a list of ParaStyle instances into a list of the
> result of applying the makeBlackAndWhite method of each instance.
>
> If I do:
> paraStyles = map( ParaStyle.makeBlackAndWhite, paraStyles )
>
> I get the following error:
> TypeError: unbound method must be called with class instance 1st argument
>
> While if I do it with a for loop:
> l = []
> for para in paraStyles:
> l.append( para.makeBlackAndWhite() )
>
> I dont get any error! =:-O
>
> What's going on???? O:-)
In the first case you call a method on a class. That's not possible.
The second case, since the list contains *instances*, everything is ok. You
call the method on the instance.
You need a lambda to do it with map:
l = map(lambda style: style.makeBlackAndWhite(), paraStyles)
The lambda creates a temporary function that calls makeBlackAndWhite on the
argument it receives.
--
Remco Gerlich
More information about the Python-list
mailing list