python - Call a function passed inside a list which is inside a class -


i novice in python , trying program game adventure.

i created class called room. in class there function called ask_something in can pass question , many list want possible answers. lists contain possible answer , effect of answer function.

how can call function whithin room class without knowing function it?

this code:

class room:     def ask_question(self, *arg):         self.question = arg[0]         self.answer_options = arg[1:]          option in self.answer_options:             print '[{}] {}'.format(self.answer_options.index(option), option[0])          answer = raw_input('> ')          self.answer_options[int(answer)][1]()  def print_this(text):     print text  room.ask_question(     'how you?',     ('fine!', print_this('ok')),     ('not fine!', print_this('i\'m sorry')) ) 

the python console says

file "room.py", line 13, in ask_question     do_something = self.answer_options[int(answer)][1]() typeerror: 'nonetype' object not callable 

you executing/calling print_this function , passing return value of executing function rather passing function itself. also, you're not creating instance of room class-- you're calling ask_question static method.

what want this:

room().ask_question(     'how you?',     ('fine!', print_this, ('ok',)),     ('not fine!', print_this, ('i\'m sorry',)) )  def ask_question(self, *arg):     #... logic missing... need handle looping through `arg` here     # example...     # first arg first tuple-- ('fine!', print_this, ('ok',))     # 2nd element of tuple print_this function     # 3rd element of tuple args pass function     do_something = arg[1][1]     do_something_args = arg[1][2]      do_something(*do_something_args) 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -