Python - Call an object from a list of objects

19,080

Solution 1

Anyone have a good way to implement a list of objects to be passed into functions? All of the objects contain the examine method, but they are objects of different classes. (I'm sorry I didn't say so earlier)

This is Python's plain duck-typing.

class Room:
    def __init__(self, name):
        self.name = name
    def examine(self):
        return "This %s looks clean!" % self.name

class Furniture:
    def __init__(self, name):
        self.name = name
    def examine(self):
        return "This %s looks comfortable..." % self.name

def examination(l):
    for item in l:
        print item.examine()

list_of_objects = [ Room("Living Room"), Furniture("Couch"), 
                    Room("Restrooms"), Furniture("Bed") ]
examination(list_of_objects)

Prints:

This Living Room looks clean!
This Couch looks comfortable...
This Restrooms looks clean!
This Bed looks comfortable...

As for your specific problem: probably you have forgotten to return a value from examine()? (Please post the full error message (including full backtrace).)

I then have a dict of functions, and I would like to pass the object to the function. However, I'm encountering some problems..For example, I have a dict:

my_dict = {'look': CallLook(rooms[i])} # this is no dict of functions

The dict you have created may evaluate to {'look': None} (assuming your examine() doesn't return a value.) Which could explain the error you've observed. If you wanted a dict of functions you needed to put in a callable, not an actual function call, e.g. like this:

my_dict = {'look': CallLook} # this is a dict of functions

if you want to bind the 'look' to a specific room you could redefine CallLook:

def CallLook(current_room)
   return current_room.examine # return the bound examine
my_dict = {'look': CallLook(room[i])} # this is also a dict of functions

Another issue with your code is that you are shadowing the built-in dict() method by naming your local dictionary dict. You shouldn't do this. This yields nasty errors.

Solution 2

Assuming you don't have basic problems (like syntax errors because the code you have pasted is not valid Python), this example shows you how to do what you want:

>>> class Foo():
...    def hello(self):
...        return 'hello'
...
>>> r = [Foo(),Foo(),Foo()]
>>> def call_method(obj):
...    return obj.hello()
...
>>> call_method(r[1])
'hello'
Share:
19,080
Ci3
Author by

Ci3

Updated on June 04, 2022

Comments

  • Ci3
    Ci3 almost 2 years

    I have a class, and I would like to be able to create multiple objects of that class and place them in an array. I did it like so:

        rooms = []
        rooms.append(Object1())
        ...
        rooms.append(Object4())
    

    I then have a dict of functions, and I would like to pass the object to the function. However, I'm encountering some problems..For example, I have a dict:

        dict = {'look': CallLook(rooms[i])}
    

    I'm able to pass it into the function, however; in the function if I try to call an objects method it gives me problems

        def CallLook(current_room)
           current_room.examine()
    

    I'm sure that there has to be a better way to do what I'm trying to do, but I'm new to Python and I haven't seen a clean example on how to do this. Anyone have a good way to implement a list of objects to be passed into functions? All of the objects contain the examine method, but they are objects of different classes. (I'm sorry I didn't say so earlier)

    The specific error states: TypeError: 'NoneType' object is not callable