1

I have this simple class in Python. Name is a string, events are list of strings.

class Page(object):

    def __init__(self, name=None, events=None):
        self.name = name
        self.events = events or []

    def add(self, x):
        return self.events.append(x)

    @property
    def eventstring(self):
        return " , ".join(self.events)

I have this input:

 log = {'key1': [['e1', 'e2'], 'e3', ]], 'key2': ['e5', 'e6', 'e7']}

I want to create read in the log and produce list of pages. i.e.

[Page('key1', ['e1', 'e2', 'e3']), Page('key2', ['e5', 'e6', 'e7']}

My current code to create the list of Page objects doesn't work. Plus, getting AttributeError: 'NoneType' object has no attribute 'append' error msg.

def inputevent(inputlog):
    final_pages = []
    pg = Page()
    for key, val in inputlog.items():
        pg.name = key
        for e in val:
            pg.events = pg.add(e)
        final_pages.append(pg)
    final_pages.append(pg)
return final_pages
1
  • What is the full traceback of the exception? Commented Mar 11, 2014 at 22:15

1 Answer 1

2

Page.add() returns None, but you are assigning it back to pg.events:

pg.events = pg.add(e)

This effectively clears pg.events. Since pg.add() alters pg.events in-place, there is no need to assign back to pg.events there at all:

for key, val in inputlog.items():
    pg.name = key
    for e in val:
        pg.add(e)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.