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
This is Python’s plain duck-typing.
Prints:
As for your specific problem: probably you have forgotten to return a value from
examine()? (Please post the full error message (including full backtrace).)The
dictyou have created may evaluate to{'look': None}(assuming yourexamine()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:
if you want to bind the
'look'to a specificroomyou could redefineCallLook:Another issue with your code is that you are shadowing the built-in
dict()method by naming your local dictionarydict. You shouldn’t do this. This yields nasty errors.