Kawu
Kawu

Reputation: 14003

How do you call a specific method on a list of instances (using map)?

I have the following class:

class Enum(RootFragment):

    def __init__(self, column, packageName, name, modifiers=["public"], enumValues=[]):
        RootFragment.__init__(self, packageName, name, modifiers, "enum")

        self.column = column
        self.enumValues = []

        map(self.addEnumValue, enumValues)

    ... more methods

Now I create a number of Enum instances that are put into a dict. This is what gets printed on print collidingEnums:

{'ManagerRole': <Enum instance at 0x0998F080>, 'StaffMemberRole': <Enum instance at 0x0998B490>}

Now because the <Enum instance at 0x0998F080> is not very useful I'd like to call the getName method on each instance. I was trying:

print ", ".join(map(Enum.getName, collidingEnums.items())),

but this gave me an error saying:

TypeError: unbound method getName() must be called with Enum instance as first argument (got tuple instance instead)

Eeh? How do you call the getName method here? Is it possible that way at all?

Upvotes: 1

Views: 205

Answers (6)

Benjamin Bannier
Benjamin Bannier

Reputation: 58604

Since on a dictionary items returns pairs (key, value) there is no method to call Enum.getName on that pair. Call it only on the value instead, e.g.

print ", ".join(map(Enum.getName, collidingEnums.values())

If you want the key displayed, too, use e.g. a lambda to work on both parts

print ", ".join(map(lambda x: x[0] +":"+ x[1].getName(), collidingEnums.items()))

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304225

You can do this with map using attrgetter

from operator import attrgetter
print ", ".join(map(attrgetter("getName"), collidingEnums.values()))

But since you are passing the result of the map into join, it would be better in this case to use a generator expression with join() as Amber suggested

Upvotes: 1

Amber
Amber

Reputation: 526743

Don't use map. Use a generator expression instead:

print ", ".join(i.getName() for i in collidingEnums.values())

Alternatively, you could simply define your own __repr__ method for your Enum class, which is what Python uses to determine what you print out if you just try to print the class.

class Enum(RootFrament):

    # ...

    def __repr__(self):
        return self.getName()

Upvotes: 4

RotaJota
RotaJota

Reputation: 841

I think a better method than using "getName" would be to use the repr, str or unicode functions on the Enum class.

It can be something like this

class Enum():
    def getName(self):
        return self.name

    def __repr__(self):
        return self.getName()

Upvotes: 1

Marcin
Marcin

Reputation: 49846

You are calling the method on a tuple. If you use collidingEnums.itervalues, you will have a generator of Enum objects.

Upvotes: 1

TJD
TJD

Reputation: 11896

If you supply the __repr__ method for you Enum class, then it will print whatever you want instead of <Enum instance at 0x0998F080>

Upvotes: 3

Related Questions