Question about map() and class methods

Jesper Hertel jh at cddk.dk
Mon Dec 20 12:20:21 EST 1999


I cannot redo the things you do.

When I run this program:

---
class foo:
    def bar(self):
        return("foo.bar")

x = map(foo.bar, [foo(), foo()])

print x
----

I get ['foo.bar', 'foo.bar'] just as expected.

Are you sure someListOfFoos are objects (instances of classes) and not
classes?



Jesper Hertel

<malraux at my-deja.com> wrote in message news:83lgi0$t$1 at nnrp1.deja.com...
> Hi all,
>
> I was attempting to use a class method with map() the other day, and
> failed miserably.
>
> Basically, if I have a class foo:
>
> class foo:
>     def bar(self):
>         return(whatever)
>
> and I try to do this:
>
> x = map(foo.bar, someListOfFoos)
>
> The interpreter complains that foo.bar requires a class as its
> argument.  Obviously, if it would only continue with the call, it would
> discover that yes, indeed, it has a whole list of them to chew on.
>
> I got around the problem by doing this:
>
> def mapFooBar(self):
>     foo.bar(self)
>
> map(mapFooBar, someListOfFoos)
>
> Which of course works fine.
>
> What am I missing here?
>
> Thanks,
>
> -scott
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.





More information about the Python-list mailing list