Skip to Content
Mastering Object-oriented Python
book

Mastering Object-oriented Python

by Steven F. Lott
April 2014
Beginner to intermediate
634 pages
15h 22m
English
Packt Publishing
Content preview from Mastering Object-oriented Python

Defining classes to support persistence

Before we can work with persistence, we need some objects that we want to save. There are several design considerations related to persistence, so we'll start with some simple class definitions. We'll look at a simple microblog and the posts on that blog. Here's a class definition for Post:

import datetime
class Post:
    def __init__( self, date, title, rst_text, tags ):
        self.date= date
        self.title= title
        self.rst_text= rst_text
        self.tags= tags
    def as_dict( self ):
        return dict(
            date= str(self.date),
            title= self.title,
            underline= "-"*len(self.title),
            rst_text= self.rst_text,
            tag_text= " ".join(self.tags),
        )

The instance variables are the attributes of each microblog post: a date, a title, some text, and some tags. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Object-Oriented Python - Second Edition

Mastering Object-Oriented Python - Second Edition

Steven F. Lott

Publisher Resources

ISBN: 9781783280971Supplemental Content