I created this notebook on OOP (object oriented programming) in Python.
Basics: classes, instances, class vs instance variables¶
In [1]:
class Blog(object):
"""Good practice (py3) is to explicitly inherit from object"""
# class variable = shared among instances
num_blogs = 0
def __init__(self, name, bio):
"""The constructor, gets called implicitly when instantiating a new object (next cell)"""
self.name = name
self.bio = bio
Blog.num_blogs += 1 # we access a class variable like this (or use a class method)
def get_articles(self, rss):
"""Get all articles from RSS"""
print('Articles {}: ...'.format(rss))
def post_article(self, title):
"""Add a new article"""
print('Posted new article: {}'.format(title))
def __str__(self):
"""Informal or nicely printable string representation of an object"""
return '{}: {}'.format(self.name, self.bio)
In [2]:
name = 'PyBites'
bio = 'Python code challenges, tutorials and news, one bite a day'
blog = Blog(name, bio)
In [3]:
blog
Out[3]: