Search code examples
pythonclassmethodsreturncalling-convention

Python - Retrieving values from methods within other classes


I am trying to teach myself Python and have created a file which runs through various questions sets spread out across classes. At the end of this file I want to summarise all of my raw inputs.

Unfortunately, I am struggling to access these values from a separate class. I have broken my coding down into a test example to demonstrate the structure of my program:

class QuestionSet(object):
    next_set = 'first_set'

class ClaimEngine(QuestionSet):

    def current_set(self):
        last_set = "blank"
        while_count = int(0)
        quizset = Sets.subsets
        ParentSet = QuestionSet()
        while ParentSet.next_set != last_set and int(while_count)<50:
            quizset[ParentSet.next_set].questioning()
            while_count = while_count+1

class FirstSet(QuestionSet):

    def questioning(self):
        value1 = raw_input("Val1")
        QuestionSet.next_set = "second_set"

class SecondSet(QuestionSet):

    def questioning(self):
        value2 = raw_input("Val2")
        QuestionSet.next_set = "summary"

class Summary(QuestionSet):
    print "test"
    ## I need to print a summary of my inputs here ##
    ## e.g. Print "The answer to value1 was:%r" %value1##

class Sets(object):
    subsets = {
    'first_set': FirstSet(),
    'second_set': SecondSet(),
    'summary': Summary()
    }

I have tried defining within the Summary each class e.g. 1stSet = FirstSet() and then FirstSet.value1 etc but to no avail.

If anyone has any suggestions on how to retrieve these values that would be great as I have written a massive program full of questions and have fallen at the last hurdle!

Thank you.


Solution

  • The values you have in each function of the classes are not created as class members. For your application you need to create member variables which store the values within the class.

    For example:

    class FirstSet(QuestionSet):
        def questioning(self):
            self.value1 = raw_input("Val1")
            QuestionSet.next_set = "second_set"
    

    Now value1 is a member variable you can access it.

    In the case of the example you have above it would probably be something like the line below to access the value1 from 'first_set'

    subsets['first_set'].value1
    

    If you're not familiar with this try this tutorial