After last post on OOP a logical follow-up is Python’s data model. We use the great Fluent Python book to code up an example of our own, showing the powerful way you can leverage this data model. You can download the notebook here.
One of the best qualities of Python is its consistency. After working with Python for a while, you are able to start making informed, correct guesses about features that are new to you.
However, if you learned another object-oriented language before Python, you may have found it strange to use len(collection) instead of collection.len(). This apparent oddity is the tip of an iceberg that, when properly understood, is the key to everything we call Pythonic. The iceberg is called the Python data model, and it describes the API that you can use to make your own objects play well with the most idiomatic language features. – Fluent Python
# for simplicity mock up some tweets
from collections import namedtuple
import random
Tweet = namedtuple('Tweet', 'time text likes')
tweets = (
Tweet('2017-01-25 08:45:00', 'Teaching Python today, feels great', 3),
Tweet('2017-01-25 09:45:00', 'Writing a post on the Python data model', 2),
Tweet('2017-01-25 07:45:00', 'from __future__ import braces ... not a chance', 10),
Tweet('2017-01-25 10:45:00', 'Doing code challenge 03, learning a lot', 5),
Tweet('2017-01-25 12:45:00', 'Done with code challenge 03', 1),
)
print(tweets)