LemusThelroy
LemusThelroy

Reputation: 303

Where attribute does not exist, create an attribute and give it a default value

I am learning Python out of a book and have written myself a long quiz/type game which prints a summary at the end. However, the summary looks for attributes that will not always exist depending on what choices have been made by the user.

I have abstracted this into a basic example to show what I am trying to do. Essentially, I just want to run an attribute error check, for every variable that does not have an attribute, create an attribute with a default value of N/A.

In the below example, I would want it to print:

Forename: Joe Surname: Bloggs Smith Test: N/A Test 4: N/A

I created a class called CodeCleaner which I was going to use to set the N/A values, but got very stuck!

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):
        self.value1 = raw_input("Forename:\n")
        QuestionSet.next_set = "second_set"

class SecondSet(QuestionSet):

    def questioning(self):
        self.value2 = raw_input("Surname:\n")
        if self.value2 == "Smith":
            self.value3 = "He's a Smith!"
            self.value4 = "Test val 4"
            QuestionSet.next_set = "summary"
        else: 
            QuestionSet.next_set = "summary"

class CodeCleaner(QuestionSet):

    def questioning(self):
        mapping = Sets() 
        sets = mapping.subsets
        variable_list = {
        [sets['first_set']].value1,
        [sets['second_set']].value2,
        [sets['second_set']].value3,
        [sets['second_set']].value4
        }
        #while key_no < 4:
        #   try:
        #       print variable_list
        #   except AttributeError:


class Summary(QuestionSet):

    def questioning(self):
        mapping = Sets() 
        sets = mapping.subsets
        print "Forename:",sets['first_set'].value1
        print "Surname:",sets['second_set'].value2
        print "Smith Test:",sets['second_set'].value3
        print "Test 4:",sets['second_set'].value4
        exit(0)

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

run = ClaimEngine()
run.current_set()

I feel quite lazy asking this question, however, I've been wrestling with this for a few days now! Any help would be appreciated.

Upvotes: 4

Views: 4140

Answers (1)

Francis Colas
Francis Colas

Reputation: 3647

I'm not sure I go exactly your approach, but you can implement a __getattr__ method in an object that would be called when the attribute is not found:

class A(object):
    def __getattr__(self, name):
        print("Creating attribute %s."%name)
        setattr(self, name, 'N/A')

Then:

>>> a = A()
>>> a.a
Creating attribute a.
>>> a.a
'N/A'

Upvotes: 4

Related Questions